I want to get the current time in this format "2018-12-10T13:34:43.5107621-05:00" by JAVA 7.
I am using EST time zone.
I want to get the current time in this format "2018-12-10T13:34:43.5107621-05:00" by JAVA 7.
I am using EST time zone.
If by EST you mean North American Eastern Time (standard or daylight time as currently used):
final ZoneId zone = ZoneId.of("America/New_York");
OffsetDateTime now = OffsetDateTime.now(zone);
String currentTimeString = now.toString();
System.out.println(currentTimeString);
When I ran this snippet just now, the output was:
2019-05-08T04:05:05.303999-04:00
If by EST you mean North American Eastern Standard Time regardless of the time of year, that is, always -05:00 (as used in Nunavut and Quintana Roo, for example):
final ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetDateTime now = OffsetDateTime.now(offset);
The rest is as before.
2019-05-08T03:10:22.788736-05:00
If a named time zone makes sense, do use it in the same way as in the first snippet, though, for example:
final ZoneId zone = ZoneId.of("America/Coral_Harbour");
The format you asked for is ISO 8601, the international standard. The classes of java.time, the modern Java date and time API generally produce this format from their toString
methods.
is ZoneId present in JAVA 7?
Edit: You have to add it. ZoneId
is part of java.time, the modern Java date and time API introduced in Java 8 and also backported to Java 6 and 7. So when using Java 7 add ThreeTen Backport to your project using the link below.
java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).