You need to learn the meaning of your input data.
The last part -0300
is likely an offset-from-UTC, a number of hours ahead of or behind UTC. I suggest use the format with a colon (-03:00
) but without is acceptable. You need to know if the plus/minus sign means ahead of or behind UTC. Modern protocols tend to use a plus for ahead of UTC and a minus for behind, but there are protocols that do the opposite.
Know that an offset is not a time zone. A time zone is a history of offsets for a particular region with rules for anomalies such as Daylight Saving Time (DST).
The first part is likely a count of milliseconds since an epoch reference date. We can guess that your epoch is the commonly used first moment of 1970 in UTC (1970-01-01T00:00:00
). But there are at least a couple dozen epochs used by various known software systems. Again, you must consult the source of your data.
This particular combination of a count-from-epoch with offset I've seen before. It confounds me as it makes more sense to simply use a count-from-epoch in UTC without an offset. If you want to show a date-time adjusted into a time zone, use the standard ISO 8601 string formats.
I will guess that your input number is a count from epoch in milliseconds in UTC. So we parse it as a Instant
object. 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).
String input = "1487615921387-0300";
String inputCount = input.substring ( 0 , 13 ); // zero-based index counting.
long count = Long.parseLong ( inputCount );
Instant instant = Instant.ofEpochMilli ( count );
We can parse the offset as a ZoneOffset
object.
String inputOffset = input.substring ( 13 );
ZoneOffset offset = ZoneOffset.of ( inputOffset );
Apply that ZoneId
to see the same moment as a wall-clock time in another offset as an OffsetDateTime
.
OffsetDateTime odt = instant.atOffset ( offset );
See this code run live at IdeOne.com.
input: 1487615921387-0300
inputMillis: 1487615921387
inputOffset: -0300
count: 1487615921387
instant.toString(): 2017-02-20T18:38:41.387Z
odt.toString(): 2017-02-20T15:38:41.387-03:00
Note the three hour difference between instant
and odt
, hours 18
versus 15
, the effect of the offset. Still the same simultaneous moment, same point on the timeline, but seen with a different wall-clock time.
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.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
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.