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;
>Solution :
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:00means seven hours ahead of UTC, as seen in Asia such as Thailand, Vietnam, Indonesia.
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.