How to format ISO-8601 in Java
I'm trying to go from the standard ISO 8601 format 2014-09-11T21:28:29.429209Z
to the nice MMM d yyyy hh: mm z format, however my current code doesn't work.
public void setCreatedAt( String dateTime ) {
LocalDate newDateTime = LocalDate.parse(dateTime);
try {
DateTimeFormatter format = DateTimeFormatter.ofPattern("MMM d yyyy hh:mm a z");
createdAt = newDateTime.format(format);
}
catch (Exception e) {
}
}
I am getting time and date from api.
source to share
A java.time.LocalDate
is "a date without a timezone in the ISO-8601 calendar system, such as 2007-12-03", so there is not enough information there. Use instead java.time.ZonedDateTime
.
Plus, swallowing such exceptions makes troubleshooting difficult. When you're not going to handle the exception, or don't catch it at all, catch and re-throw it wrapped in a RuntimeException, or at least register it ( e.printStackTrace()
or similar).
source to share
TL; DR
Instant.parse( "2014-09-11T21:28:29.429209Z" )
.atZone( ZoneId.of( "Asia/Kolkata" ) )
.format( DateTimeFormatter.ofPattern("MMM d uuuu hh:mm a z" , Locale.US ) )
See this code run at IdeOne.com .
More details
Jakber's answer will work technically but is misleading.
ZonedDateTime
for time zones
A ZonedDateTime
has a designated time zone such as America/Montreal
or Pacific/Auckland
.
But there is no time zone in this input line. Z
at the end for a short Zulu
and means UTC, or in other words, the displacement from the zero-UTC hours +00:00
.
A time zone is a historical set of offsets for a particular region, with rules for upcoming offset changes for anomalies such as Daylight Saving Time (DST) .
Instant
The input string is better parsed as Instant
, which represents a moment on the timeline, always in UTC. This class parses such strings directly in this ISO 8601 standard so there is no need for template formatting.
Instant instant = Instant.parse( "2014-09-11T21:28:29.429209Z" );
Timezone setting
You can adjust the time zone to receive ZonedDateTime
.
ZoneId z = ZoneId.of( "Asia/Kolkata" ) ;
ZonedDateTime zdt = instant.atZone( z );
Generating a string
To generate a string in the same format as the input, just call Instant::toString()
.
String output = instant.toString() ;
For your custom format using wall clock times in your region, use DateTimeFormatter
with your custom formatting template. And yours ZonedDateTime
. As a good habit, always indicate the desired language used for human language in translation and cultural norms when formatting.
DateTimeFormatter f = DateTimeFormatter.ofPattern("MMM d uuuu hh:mm a z" , Locale.US );
String output = zdt.format( f );
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the nasty old legacy time classes such as java.util.Date
, Calendar
and . SimpleDateFormat
The Joda-Time project , now in maintenance mode , advise moving to the java.time classes .
To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification JSR 310 .
Where can I get the java.time classes?
- Java SE 8 and SE 9 and later
- Built in.
- Part of the standard Java API with a combined implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Most of the functionality of java.time has been ported to Java 6 and 7 in ThreeTen-Backport .
- Android
- The ThreeTenABP project adapts the ThreeTen-Backport (mentioned above) specifically for Android.
- See. How to use ... ThreeTenABP .
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes, such as Interval
, YearWeek
, YearQuarter
and longer .
source to share