Already, the root cause has been pointed out and the resolution has been suggested in the accepted answer. This answer is to introduce the modern Date-Time API to future visitors.
java.time
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Solution using java.time
, the modern Date-Time API:
import java.time.Instant;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println(instant);
}
}
Output from a sample run:
2021-07-03T14:08:02.594203Z
ONLINE DEMO
An Instant
represents an instantaneous point on the timeline in UTC. The Z
in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC
timezone (which has the timezone offset of +00:00
hours).
Note: In case you have got an object of java.util.Date
, you can convert it to Instant
as shown below:
Date date = new Date(); // A sample date
Instant instant = date.toInstant();
How to display the current Date-Time in a particular timezone:
Use ZonedDateTime#now
or ZonedDateTime#now(ZoneId)
to display the current Date-Time in a particular timezone.
Demo:
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
// The current date-time in the JVM's timezone
ZonedDateTime zdtDefaultTz = ZonedDateTime.now();
System.out.println(zdtDefaultTz);
// The current date-time in a specific timezone
ZonedDateTime zdtNewYork = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println(zdtNewYork);
}
}
Output from a sample run:
2021-07-03T15:19:48.007549+01:00[Europe/London]
2021-07-03T10:19:48.010048-04:00[America/New_York]
ONLINE DEMO
How to display the current Date-Time in a particular timezone if you have got an instance of java.util.Date
or Instant
:
Convert Instant
to ZonedDateTime
representing Date-Time in the required timezone e.g.
ZonedDateTime zdt = instant.atZone(ZoneId.of("America/New_York"));
Demo:
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
Instant instant = Instant.now(); // new Date().toInstant()
ZonedDateTime zdtDefaultTz = instant.atZone(ZoneId.systemDefault());
System.out.println(zdtDefaultTz);
ZonedDateTime zdtNewYork = instant.atZone(ZoneId.of("America/New_York"));
System.out.println(zdtNewYork);
ZonedDateTime zdtUtc = instant.atZone(ZoneId.of("Etc/UTC"));
System.out.println(zdtUtc);
}
}
Output:
2021-07-03T15:24:23.716050+01:00[Europe/London]
2021-07-03T10:24:23.716050-04:00[America/New_York]
2021-07-03T14:24:23.716050Z[Etc/UTC]
ONLINE DEMO
Learn more about the modern Date-Time API from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.