1

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;
Atihska
  • 4,803
  • 10
  • 56
  • 98
  • 5
    it's [ISO 8601 international date format](https://en.wikipedia.org/wiki/ISO_8601) – phuclv Apr 02 '22 at 02:39
  • 1
    @phuclv Thanks for your quick reply. So 0700 = GMT - 7 ? – Atihska Apr 02 '22 at 02:44
  • 1
    It is probably it is UTC - 7. But in theory, you don't know what an "unknown" date format actually represents unless someone / something tells you. (Who knows what weird funkiness the code that generated the date might do? Who knows if (say) their clock was out of sync by (say) 30 minutes?) – Stephen C Apr 02 '22 at 02:45
  • @StephenC So I know its GMT for sure (as someone has told me explicitly). – Atihska Apr 02 '22 at 02:53
  • 1
    Well ... they are explicitly incorrect. Timezones are NOT defined relative to GMT. They are defined relative to UTC. Please read the link provided by phuclv above. – Stephen C Apr 02 '22 at 02:57
  • 1
    The difference between GMT and UTC is irrelevant in common business-oriented situations. Not worth quibbling over unless you are doing rocket science or GPS/Galileo satellite calculations. – Basil Bourque Apr 02 '22 at 03:23

1 Answers1

5

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154