How To Convert Rfc-1123 Date-time Formatter, 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?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
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"