-6

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.

Ayush
  • 206
  • 7
  • 22
  • I can guess what the other fields are, but what information does "5107621" contain? – Torben May 06 '19 at 12:29
  • If you are using Java 7, your options are to use `SimpleDateFormat` (and the appropriate pattern) or a 3rd-party library such as Joda time. With Java 8, the recommended approach is to use `DateTimeFomatter`. Details are in the respective javadocs. Look them up! – Stephen C May 06 '19 at 12:52
  • Read this : https://stackoverflow.com/questions/2201925/converting-iso-8601-compliant-string-to-java-util-date – Paul May 06 '19 at 12:53
  • @Paul.F-G It’s the opposite conversion, but may be more or less helpful. I am sure that a search will bring up more helpful questions and web pages. – Ole V.V. May 06 '19 at 18:59
  • Simple as `OffsetDateTime.now(ZoneId.of("America/New_York")).toString()`. Assuming that by EST you mean North American Eastern Time (either standard or daylight as appropriate). Does it work in Java 7? Certainly! These java.time classes have been backported, so when you add [ThreeTen Backport](https://www.threeten.org/threetenbp/) to your project, it works. – Ole V.V. May 06 '19 at 19:04
  • 2
    @Torben The format is [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). The .5107621 is the fraction of second (though you may not be able to obtain an accuracy of 7 decimals). – Ole V.V. May 06 '19 at 19:08
  • Possible duplicate of [How to get current moment in ISO 8601 format with date, hour, and minute?](https://stackoverflow.com/questions/3914404/how-to-get-current-moment-in-iso-8601-format-with-date-hour-and-minute) – Ole V.V. May 06 '19 at 19:09

1 Answers1

0

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.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161