Search code examples
androidandroid-calendarandroid-date

How to convert date time from one format to another in Android?


I want to convert date and time from "2014-11-25 14:30" format to " Nov 25th,2014 at 2:30pm ". Could anybody please tell how to do so?

Thanks!


Solution

  • Try with below code:

    String date = "2014-11-25 14:30";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:MM");
            Date testDate = null;
            try {
                testDate = sdf.parse(date);
            }catch(Exception ex){
                ex.printStackTrace();
            }
            SimpleDateFormat formatter = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
            String newFormat = formatter.format(testDate);
            System.out.println(".....Date..."+newFormat);