user9300680
user9300680

Reputation:

Java GET returning date as a 13 digit number

My web application returns a date as a 13 digit number, such as 1475166540000.

How to format it into a proper date to be returned in the JSON response. In my function it just returns the whole DTO.

Upvotes: 4

Views: 8173

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 339332

tl;dr

Instant.ofEpochMilli( 1_475_166_540_000L )
       .toString()

Or…

Instant.ofEpochMilli( Long.parseLong( "1475166540000" ) )
       .toString()

Details

An Instant represents a moment in UTC with a resolution of nanoseconds.

That class can parse a count of milliseconds from the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z.

Instant instant = Instant.ofEpochMilli( 1_475_166_540_000L ) ;

Ditto for count of whole seconds, by the way, Instant.ofEpochSecond( mySeconds ).

If the input is a String, parse into a long.

Instant instant = Instant.ofEpochMilli( Long.parseLong( "1475166540000" ) ) ;

To generate a String in standard ISO 8601 format, call toString.

String output = instant.toString() ;

2016-09-29T16:29:00Z

The Z on the end is short for “Zulu” and means UTC.

How to format it into a proper date

No such thing as a “proper date”. Every culture has their own way of textually displaying date-time values.

For localized display to users, use DateTimeFormatter and its ofLocalized… methods.

String output = instant.atZone( ZoneId.of( "Africa/Tunis" ) ).format( DateTimeFormatter.ofLocalizedDateTime​( FormatStyle.FULL ).withLocale( Locale.JAPAN ) ) ;  // Use Japanese formatting to display a moment in Tunisia wall-clock time.

2016年9月29日木曜日 17時29分00秒 中央ヨーロッパ標準時

For exchanging data between systems such as JSON, use only the standard ISO 8601 formats. They are practical, designed to be unambiguous and easy to parse by machine as well as easy to read by humans across cultures. The java.time class built into Java use these standard formats by default when parsing/generating strings. So no need to specify a formatting pattern.

String output = instant.toString() ;

2016-09-29T16:29:00Z

Instant instant = Instant.parse( "2016-09-29T16:29:00Z" ) ;
long millisSinceEpoch = instant.toEpochMilli() ;

1475166540000

Avoid legacy date-time classes

Never use the troublesome Date/Calendar classes seen in your example code. They are now legacy, supplanted entirely by the java.time classes.

To inter-operate with old code not yet updated to java.time, you can convert back and forth. Call on New conversion methods added to the old classes.

Date d = Date.from( instant ) ;  // Don’t do this unless forced to inter-operate with old code not yet updated to java.time classes.

Table of all date-time types in Java, both modern and legacy


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.

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

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

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. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Upvotes: 7

xxis
xxis

Reputation: 11

That 13-digit number is a Unix timestamp: https://en.wikipedia.org/wiki/Unix_time It's the number of milliseconds since Jan 1st 1970, at midnight, in UTC.

To convert it to a Date, just do:

Date date = new Date(Long.parseLong("1475166540000"));

If you use Java 8 or higher, refer to the other answers

Upvotes: 1

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60016

If you are using Java 8 you can use java.time library :

String date = LocalDateTime.ofInstant(
        Instant.ofEpochMilli(Long.valueOf("1475166540000")), ZoneId.systemDefault()
).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

System.out.println(date);// 2016-09-29 17:29:00

Upvotes: 1

Related Questions