Sadda Hussain
Sadda Hussain

Reputation: 373

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.

Upvotes: 1

Views: 7764

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 338654

tl;dr

Use java.time classes.

Instant.ofEpochSecond( 1_485_748_890L )
       .atZone( ZoneId.of( "America/Montreal" ) )
       .toLocalDate()
       .format(    
           DateTimeFormatter.ofPattern ( "dd-MM-uuuu" )
                            .withLocale ( Locale.UK )
       )

29-01-2017

Joda-Time

If you want a date-only value without a time-of-day, you should be using the org.joda.time.LocalDate class.

FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. Much of java.time is back-ported to Java 6, Java 7, and Android (see below).

java.time

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate birthdate = LocalDate.of( 1994 , 5 , 10 );

If your input is a count of whole seconds since the epoch of first moment of 1970 in UTC (1970-01-01T00:00:00Z), convert to an Instant. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.ofEpochSecond( 1_485_748_890L );

To view this moment through the lens of a particular region’s wall-clock time, assign a time zone (ZoneId) to get a ZonedDateTime.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = zdt.toLocalDate();

To generate a String representing the value of your object, call toString for a string in standard ISO 8601 format, YYYY-MM-DD.

ld.toString(): 2017-01-29

For other formats, use the DateTimeFormatter class. You can specify a formatting pattern, or let the class automatically localize.

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "dd-MM-uuuu" ).withLocale ( Locale.UK );
String output = ld.format ( f );

instant.toString(): 2017-01-30T04:01:30Z

zdt.toString(): 2017-01-29T23:01:30-05:00[America/Montreal]

ld.toString(): 2017-01-29

output: 29-01-2017

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

Upvotes: 1

Saurabh
Saurabh

Reputation: 1245

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

Upvotes: 0

rafsanahmad007
rafsanahmad007

Reputation: 23881

try this:

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS zzz");
// pass your DOB String
DateTime jodatime = dtf.parseDateTime(DOB);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("dd-MM-yyyy");
// Print the date
System.out.println(dtfOut.print(jodatime));

Upvotes: 1

Related Questions