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.

+3


source to share


2 answers


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).

+2


source


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?

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 .

+1


source







All Articles