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.