我遇到了帶有時區的日期格式,GMT但我不明白上述日期中微秒后的連字符部分是什么,以及兩種方式決議的格式是什么?
所以對于這個日期(沒有連字符):,"2021-08-03T04:10:07.502"格式是YYYY-MM-dd'T'HH:mm:ss.SS
Q1:對于這個日期(帶連字符):,"2021-08-03T04:10:07.502-0700"格式為:??
Q2:連字符部分是時區GMT嗎?
Q3:如果日期是微秒后的連字符形式,如何添加X個數字來解決它?
預期的Java代碼:
String dateFormatWithHyphen = "?"; // replace ? with that format
DateFormat dateFormat = new SimpleDateFormat(dateFormatWithHyphen);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat;
uj5u.com熱心網友回復:
tl;博士
輸入字串的圖表,包含日期、分隔符、時間和與 UTC 的偏移量。
2021-08-03T04:10:07.502-0700
^date^ ^ ^time^ ^offset
separator
將您的日期與時間和從 UTC 的偏移量作為java.time.OffsetDateTime物件決議。
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:對于這個日期(帶連字符):“2021-08-03T04:10:07.502-0700”,格式為:??
該字串采用標準ISO 8601格式。
但是,該字串省略了從 UTC 偏移的小時和分鐘之間的可選冒號字符。我建議始終包含該冒號,以實作機器的最大兼容性。冒號使它對人類來說也更具可讀性。所以使用這個:
2021-08-03T04:10:07.502-07:00
Q2:連字符部分是 GMT 時區嗎?
前面的 HYPHEN-MINUS 字符0700表示七個小時的偏移量在UTC 時間本初子午線之后。
- -07:00表示比 UTC 晚 7 小時,如在美洲所見。
07:00意味著比 UTC 提前 7 小時,如泰國、越南、印度尼西亞等亞洲地區所見。
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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/455087.html
下一篇:如何在Java中對條目進行排序
