[英]Parse String to Date format in Spring
我正在嘗試更新 spring 類中的谷歌日歷事件開始和結束日期時間。但我收到 DateFormat 異常。我能夠獲取特定事件並在模式彈出窗口中顯示。我正在使用劍道指令 datepicker。現在如果用戶編輯開始和結束日期正在以這種格式更改
String start=2/21/2018 8:00 PM
String end:2/22/2018 9:00 PM
現在我正在嘗試在我的課堂上像這樣為谷歌事件開始和結束日期設置這些日期
ServiceImpl.java
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try {
Date d = sdf.parse(start);//2/21/2018 8:00 PM
Date end = sdf.parse(end);//2/22/2018 9:00 PM
event.setStart(new EventDateTime().setDateTime(new DateTime(d)));
event.setEnd(new EventDateTime().setDateTime(new DateTime(end)));
// Update the event
Event updatedEvent = service.events().patch("primary", googleEventDto.getEventId(), event).execute();
} catch (ParseException e)
{
// execution will come here if the String that is given
// does not match the expected format.
e.printStackTrace();
}
但是我收到異常 Unparseable date 2/21/2018 8:00 PM。但是如果像這樣直接設置
event.setStart(new EventDateTime().setDateTime(new DateTime("2018-02-28T07:00:00.000+05:30")));
event.setStart(new EventDateTime().setDateTime(new DateTime("2018-02-28T09:00:00.000+05:30")));
它工作得很好。誰能告訴我如何將日期 2/21/2018 8:00 解析為這種格式 2018-02-21T08:00:00.000+05:30?
我認為您使用的DateTime
類是com.google.api.client.util.DateTime
。 如果是這樣,以下方法將解決您的問題:
private static final DateTimeFormatter userDateTimeFormatter
= DateTimeFormatter.ofPattern("M/d/uuuu h:mm a", Locale.ENGLISH);
private static final ZoneId zone = ZoneId.of("Asia/Kolkata");
private static DateTime parseToGoogleDateTime(String userDateTime) {
long millis = LocalDateTime.parse(userDateTime, userDateTimeFormatter)
.atZone(zone)
.toInstant()
.toEpochMilli();
return new DateTime(millis);
}
像這樣使用:
try {
DateTime startDateTime = parseToGoogleDateTime(start); //2/21/2018 8:00 PM
DateTime endDateTime = parseToGoogleDateTime(end); //2/22/2018 9:00 PM
event.setStart(new EventDateTime().setDateTime(startDateTime));
event.setEnd(new EventDateTime().setDateTime(endDateTime));
// Update the event
Event updatedEvent = service.events().patch("primary", googleEventDto.getEventId(), event).execute();
} catch (DateTimeParseException dtpe) {
// execution will come here if the String that is given
// does not match the expected format.
dtpe.printStackTrace();
}
編輯:我知道你的日期時間字符串也可能像2018-02-28T07:00:00.000+05:30
(ISO 8601 格式)。 您可能想看看是否可以修復它,以便只有一種格式是可能的; 但如果沒有,請嘗試依次解析兩種格式,直到一種有效:
private static DateTime parseToGoogleDateTime(String userDateTime) {
long millis;
try {
// try to parse fprmat like 2018-02-28T07:00:00.000+05:30
millis = OffsetDateTime.parse(userDateTime)
.toInstant()
.toEpochMilli();
} catch (DateTimeParseException dtpe) {
// instead try like 2/21/2018 8:00 PM
millis = LocalDateTime.parse(userDateTime, userDateTimeFormatter)
.atZone(zone)
.toInstant()
.toEpochMilli();
}
return new DateTime(millis);
}
我正在使用並熱烈推薦 java.time,現代 Java 日期和時間 API。 您使用的日期時間類Date
和SimpleDateFormat
早已過時,而后者尤其麻煩。 所以我在我的代碼中避免使用它們。 現代 API 更易於使用。
如果不是亞洲/加爾各答,請替換您想要的時區。 您可以將ZoneId.systemDefault()
用於 JVM 的時區設置,只要知道該設置可能會被程序的其他部分或在同一 JVM 中運行的其他程序更改。 由於 AM 和 PM 幾乎不用於英語以外的其他語言,因此請提供英語語言環境。
鏈接: Oracle 教程:解釋如何使用java.time
的日期時間。
如果你想解析格式的日期:2/21/2018 8:00 PM 你必須將模式更改為:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
有關更多信息,請參閱SimpleDateFormat 的 javadoc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.