androidjodatimeandroid-jodatime

How to format Joda Time to dd-mm-yyyy in android?


 String DOB = new DateTime(Long.parseLong(dob) * 1000, DateTimeZone.UTC ).toString();
 // Current
 // YYYY-MM-DD
 // DOB = "1994-05-10T00:00.000Z"

 // Required 
 // DD-MM-YYYY
 // DOB = "10-05-1994"

I want to remove the hh:mm:ss and format the date using Joda-Time DateTimeFormatter.


Solution

  • try this :

    String textDate ="1994-05-10T00:00.000Z"; //Date to convert
    
    DateTimeFormatter DATE_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ"); //Default format
    
    SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()); //Needed format
    
    DateTime dateTime = new DateTime(DATE_FORMAT.parseDateTime(textDate),        DateTimeZone.forID(current.getID()));
    
    Calendar cal=dateTime.toCalendar(Locale.getDefault());
    
    String formatted = SIMPLE_DATE_FORMAT.format(cal.getTime()); //Final Required date