1

I'm trying to convert a Long timestamp with an offset (int) into a more readable format. So for example I have 1514564669355 as a timestamp with offset equal to 360. How should I go about transforming this into the equivalent date format using java?

In this case the timestamp would be stored in UTC, so with the offset I'm looking to converting it to whatever timezone it needs. Thanks for any help/tips.

Tuco
  • 902
  • 3
  • 18
  • 33
  • sure of youre timestamp ? What date it is supposed to be ? – azro May 08 '18 at 16:33
  • 1
    Its a Unix timestamp stored in DynamoDB, you can use https://www.epochconverter.com/ to convert it. I'm jsut not sure how to handle these in Java with an int offset. – Tuco May 08 '18 at 19:04
  • I'm not used to "offset" in timestamp, what is it ? – azro May 08 '18 at 19:09
  • The offset would be used to convert the timestamp to a certain timezone. Each timezone has its own offset. – Tuco May 08 '18 at 19:19
  • If you can, for presentation to a user in a given time zone, get a proper time zone like Asia/Almaty or Asia/Urumqi rather than just an offset. – Ole V.V. May 09 '18 at 07:03

1 Answers1

4

Your Question is not clear.

  • Perhaps you mean you have a value in UTC and want to adjust it to an offset of 360 minutes ahead of UTC. (A poor way to communicate a moment in time.)
  • Or perhaps you meant the given value is already ahead of UTC. (An even worse way to communicate a moment in time.)

Starting with UTC

First parse your input number. We will assume that number is a count of milliseconds since the first moment of 1970 UTC, 1970-01-01T00:00Z.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.ofEpochMilli( 1_514_564_669_355L ) ;

instant.toString(): 2017-12-29T16:24:29.355Z

An offset-from-UTC is a number of hours, minutes, and seconds ahead of, or behind, UTC.

Perhaps your Question’s mention of an offset of 360 is meant to be 360 minutes, or 6 hours ahead of UTC.

The ZoneOffset class cannot be instantiated from a number of minutes over 59. So we convert from 360 minutes to a total number of seconds.

int seconds = ( int ) TimeUnit.MINUTES.toSeconds( 360L ) ;  // Convert minutes to seconds.
ZoneOffset offset = ZoneOffset.ofTotalSeconds( seconds );   // Determine offset-from-UTC.

offset.toString(): +06:00

Adjust our Instant in UTC to this offset, yielding a OffsetDateTime object. Same moment, same point on the timeline, different wall-clock time. So the time-of-day is seen as 10 PM rather than as 4 PM since ( 16 + 6 ) = 22.

    OffsetDateTime odt = instant.atOffset( offset );

odt.toString(): 2017-12-29T22:24:29.355+06:00

Starting with offset

Perhaps you meant the moment being communicated is a count of milliseconds from UTC but then the number of milliseconds in 360 minutes has already been added or subtracted.

By the way, this is a very bad way to exchange date-time values. Educate the supplier of your data about the ISO 8601 standard.

Let's undo that addition/subtraction of an offset, to get back to UTC.

long millisInOffset = TimeUnit.MINUTES.toMillis( 360L );  // Convert minutes to milliseconds.
long millisSinceEpoch = ( 1_514_564_669_355L - millisInOffset );
Instant instant = Instant.ofEpochMilli( millisSinceEpoch );

instant.toString(): 2017-12-29T10:24:29.355Z

See that value in the offset sent to us.

int seconds = ( int ) TimeUnit.MILLISECONDS.toSeconds( millisInOffset );
ZoneOffset offset = ZoneOffset.ofTotalSeconds( seconds );
OffsetDateTime odt = instant.atOffset( offset );
System.out.println( odt );

2017-12-29T16:24:29.355+06:00


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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154