Home > Enterprise >  What date format is this date "2021-08-03T04:10:07.502-0700"?
What date format is this date "2021-08-03T04:10:07.502-0700"?

Time:04-02

I encountered a date format with timezone as GMT but I don't understand what is the hyphen part after microseconds in the above date and what will be its format to parse both ways?

So for this date (without hyphen): "2021-08-03T04:10:07.502", the format is YYYY-MM-dd'T'HH:mm:ss.SS

Q1: And for this date (with hyphen): "2021-08-03T04:10:07.502-0700", the format is: ??

Q2: Is the hyphen part the timezone GMT?

Q3: If the date is in with the hyphen form after microseconds, how can one add X number of digits to address it?

Prospective Java code:

String dateFormatWithHyphen = "?"; // replace ? with that format
DateFormat dateFormat = new SimpleDateFormat(dateFormatWithHyphen);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat;

CodePudding user response:

tl;dr

Diagram of your input string, containing a date, a separator, a time-of-day, and an offset-from-UTC.

2021-08-03T04:10:07.502-0700
  ^date^  ^   ^time^   ^offset
       separator

Parse your date with time-of-day and offset-from-UTC as an java.time.OffsetDateTime object.

OffsetDateTime
.parse( 
    "2021-08-03T04:10:07.502-0700" , 
    new DateTimeFormatterBuilder()
    .parseLenient()
    .append( DateTimeFormatter.ISO_LOCAL_DATE_TIME )
    .appendPattern( "xx" )
    .toFormatter()
)
.toString()

2021-08-03T04:10:07.502-07:00

Q1: And for this date (with hyphen): "2021-08-03T04:10:07.502-0700", the format is: ??

That string is in standard ISO 8601 format.

However, that string omits the optional COLON character between the hours and minutes of the offset-from-UTC. I would suggest always including that COLON for maximum compatibility in machines. And the COLON makes it more readable too for humans. So use this:

2021-08-03T04:10:07.502-07:00

Q2: Is the hyphen part the timezone GMT?

The HYPHEN-MINUS character in front of 0700 means the offset of seven hours is behind the temporal prime meridian of UTC.

-07:00 means seven hours behind UTC, as seen in the Americas.

  • 07:00 means seven hours ahead of UTC, as seen in Asia such as Thailand, Vietnam, Indonesia.

UTC is the new GMT, practically speaking, with regard to common business-oriented situations. If you are doing rocket science or GPS/Galileo satellite calculations, you should research the difference. If you are programming for purchase orders and invoices, don't worry about it.

Regarding your phrase, “the timezone GMT”… that is a contradiction. UTC/GMT is not a time zone. It is the baseline against which offsets are defined: a certain number of hours-minutes-seconds. What longitude is to the prime meridian, offsets are to UTC. Time zones are much more. Time zones are a named history of the past, present, and future changes to the offset used by the people of a particular region as decided by politicians.

Q3: If the date is in with the hyphen form after microseconds, how can one add X number of digits to address it?

Actually, the .502 is milliseconds, not microseconds.

And no, the date is up front, the 2021-08-03 part, August 3rd, 2021.

The T separates the date portion from the time portion. The third portion is the offset of -07:00.

Code

You said:

DateFormat dateFormat = new SimpleDateFormat(dateFormatWithHyphen); dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in java.time. Never use Date, Calendar, SimpleDateFormat, and such.

Use OffsetDateTime to represent a date with time-of-day as seen with a particular offset-from-UTC.

If your input included the optional COLON, we could simply do this:

String input = "2021-08-03T04:10:07.502-07:00" ;
OffsetDateTime odt = OffsetDateTime.parse( input ) ;

Without the COLON, we must specify a formatting pattern. We can build up a DateTimeFormatter object by using a DateTimeFormatterBuilder.

DateTimeFormatter f = 
    new DateTimeFormatterBuilder()
    .parseLenient()
    .append( DateTimeFormatter.ISO_LOCAL_DATE_TIME )
    .appendPattern( "xx" )
    .toFormatter()
;

Use that formatter.

OffsetDateTime odt = OffsetDateTime.parse( input , f ) ;

odt.toString(): 2021-08-03T04:10:07.502-07:00

Well, that code works. But the ideal solution would be convincing the publisher of your data to use the full ISO 8601 format including the COLON in every offset.

  • Related