Skip to content Skip to sidebar Skip to footer

How To Convert Rfc-1123 Date-time Formatter, To Local Time

I am getting the date-time Thu, 25 Aug 2016 08:59:00 GMT in this RFC 1123 format in my Android app. I need to convert to local time.

Solution 1:

DateTimeFormatter.RFC_1123_DATE_TIME

The java.time classes can directly parse/generate strings in the RFC 1123 via the DateTimeFormatter.RFC_1123_DATE_TIME constant.

ZonedDateTimezdt= 
    ZonedDateTime.parse( 
        "Thu, 25 Aug 2016 08:59:00 GMT" ,  
        DateTimeFormatter.RFC_1123_DATE_TIME 
    ) 
;

From there, adjust into your desired time zone.

ZoneIdz= ZoneId.of( "America/Montreal" );  // Or "Asia/Kolkata", etc.ZonedDateTimezdtMontreal= zdt.withZoneSameInstant( z );

FYI, that RFC 1123 format is terrible, and is outdated nowadays. The format’s problems include: assumes English, difficult to parse, and uses the 3-4 letter zone abbreviations that are not real time zones, not standardized, and are not even unique(!). Instead:

  • Specify time zones using proper IANA-defined time zones.
  • Serialize to text using standard ISO 8601 formats.The java.time classes use these formats by default when parsing/generating textual representations of date-time values.

Search Stack Overflow for more information about adjusting time zones and generating Strings of date-time values.


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.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Post a Comment for "How To Convert Rfc-1123 Date-time Formatter, To Local Time"