-1

What would be the best way to return current date and time in this format?

2021-05-16T09:20:47-05:00

Currently I am doing this:

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
strDate = formatter.format(date);

But I believe there are better ways of accomplishing what I am trying.

EDIT:

OffsetDateTime.now() does the job.

A.J
  • 1,140
  • 5
  • 23
  • 58
  • 4
    Look at the modern `java.time` API. – Andy Turner Apr 26 '21 at 12:33
  • 1
    Thanks, `OffsetDateTime.now()` worked. – A.J Apr 26 '21 at 12:43
  • 1
    You should add an answer, not writing it as comment or as edit of an answer. There is no problem to answer own question. Just: write an example and example of output (not just few words). Remember: this is a reference site, so questions and answers are supposed to be useful to many people (not just the original author) – Giacomo Catenazzi Apr 26 '21 at 12:46
  • @AndyTurner want to write an answer so I can accept it? – A.J Apr 26 '21 at 13:12

2 Answers2

1

Try use (since Java 8) ZonedDateTime formated with DateTimeFormatter. DateTimeFormatter object can be created and configurated with DateTimeFormatterBuilder class:

System.out.println(ZonedDateTime.now()
                     .format(new DateTimeFormatterBuilder()
                                  .append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
                                  .appendOffsetId()
                                  .toFormatter()));
Sergey Afinogenov
  • 2,137
  • 4
  • 13
  • `OffsetDateTIme` is a more accurate choice than `ZonedDateTime` in this case since we need a UTC offset (and not a time zone like Europe/Moscow). – Ole V.V. Apr 26 '21 at 18:30
1

java.time.OffsetDateTime

Like the others I clearly recommend that you use java.time, the modern Java date and time API, for your date and time work.The class you need for a date and time with an offset such as -05:00 is OffsetDateTime (not that surprising, is it?)

    ZoneId zone = ZoneId.of("America/Monterrey");
    OffsetDateTime currentDateAndTime = OffsetDateTime.now(zone);
    System.out.println(currentDateAndTime);

Output when I ran the code just now:

2021-04-26T13:22:05.246327-05:00

If you want the offset for your own time zone, set zone to ZoneId.systemDefault().

I am exploiting the facts that you were asking for ISO 8601 format and the classes of java.time produce ISO 8601 format from their toString methods, so without any explicit formatter.

So if you need a String, it’s just:

    String currentDateAndTimeString = currentDateAndTime.toString();
    System.out.println(currentDateAndTimeString);

Output is the same as before.

If you need a string with seconds and without fraction of second — you most probably don’t. As I wrote, the format that you asked for is ISO 8601, and according to the ISO 8601 standard the seconds and fraction of seconds are optional and understood to be zero when they are absent. So assuming that you need this string for an external service or other component requiring ISO 8601, the above string is fine. In any case: Edit: Truncate the fraction of second away and use the standard formatter:

    String currentDateAndTimeWithSecondsOnly = currentDateAndTime
            .truncatedTo(ChronoUnit.SECONDS)
            .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    System.out.println(currentDateAndTimeWithSecondsOnly);

Output when running just now:

2021-04-26T23:53:22-05:00

DateTimeFormatter.ISO_OFFSET_DATE_TIME will output the seconds even if they are zero, and will leave out the fraction of second when it is zero (I needed to read the documentation very carefully to understand this).

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Thanks for accepting the answer. I just determined that the standard formatter does print seconds, to that one and a truncation do the job with printing without the decimal fraction. I have edited and simplified the last part of the answer accordingly. – Ole V.V. Apr 27 '21 at 04:58