0

I am trying to use Java to format a time in milliseconds into a date in UTC. I have the following code:

long ms = 1427590800000;
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT);
cal.setTimeInMillis(ms);
Date date = cal.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
System.out.println(dateFormat.format(date)); // 2015-03-29 02:00:00

This is printing a time in BST (i.e. using the default time-zone) rather than UTC. It seems like the time-zone being set on the calendar has no bearing on the date being printed.

The actual time in UTC is shown by the following python snippet:

import datetime
ms = 1427590800000
print datetime.datetime.utcfromtimestamp(ms/1000.0) # 2015-03-29 01:00:00

Setting the default JVM time-zone to "UTC" results in the correct date being printed, but this doesn't seem like a safe solution.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • possible duplicate of [java convert milliseconds to time format](http://stackoverflow.com/questions/4142313/java-convert-milliseconds-to-time-format). **Please search StackOverflow** before posting. This topic has been addressed *many* times already. – Basil Bourque Jul 02 '15 at 21:10
  • The linked question makes no mention of UTC, and the accepted answer doesn't have the required step of setting the time-zone on the format. – Kim Barrett Jul 13 '15 at 08:55
  • Duplicate of [Java it to milliseconds](http://stackoverflow.com/q/20158632/642706), and duplicate of [Date in to UTC format Java](http://stackoverflow.com/questions/20238280/date-in-to-utc-format-java) and many many more. I'll ask again, please search StackOverflow before posting. – Basil Bourque Jul 13 '15 at 15:00

2 Answers2

1

java.time

The modern approach uses the industry-leading java.time classes.

Parse as an Instant, a moment in UTC with a resolution of nanoseconds.

long input = 1_427_590_800_000L ;
Instant instant = Instant.ofEpochMilli( input ) ;

ISO 8601

Generate a string in standard ISO 8601 format.

String output = instant.toString() ;

2015-03-29T01:00:00Z


Table of 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.

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
0

You need to set the timezone to the formatter before formatting if you want a desired timezone.

Use dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); and then call dateFormat.format(date)

Codebender
  • 14,221
  • 7
  • 48
  • 85