将字符串解析为 Spring 中的日期格式
Parse String to Date format in Spring
我正在尝试更新 google 中的日历事件开始和结束日期时间 spring class.But 我正在获取 DateFormat exception.I 我能够获取特定事件并显示在模态 popup.I 我正在使用 kendo 指令 datepicker.Now 如果用户编辑开始和结束日期,它将以这种格式更改
String start=2/21/2018 8:00 PM
String end:2/22/2018 9:00 PM
现在我正尝试在 class
中像这样设置 google 事件开始和结束日期
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.But 如果像这样直接设置
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")));
正在运行 perfectly.Can 谁能告诉我如何将日期 2/21/2018 8:00 解析为这种格式 2018-02-21T08:00:00.000+05:30?
如果你想解析格式的日期:2/21/2018 8:00 PM 你必须将模式更改为:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
有关详细信息,请参阅 javadoc of SimpleDateFormat
我认为您使用的DateTime
class是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();
}
编辑: 我知道您的 date-time 字符串也可能像 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-time classes,Date
和 SimpleDateFormat
早已过时,而后者尤其麻烦。所以我在我的代码中避免使用它们。现代 API 更好用。
如果不是 Asia/Kolkata,请替换为您想要的时区。您可以使用 ZoneId.systemDefault()
作为 JVM 的时区设置,只是要知道该设置可能会被您程序的其他部分或同一 JVM 中的其他程序 运行 更改。由于 AM 和 PM 几乎不用于英语以外的其他语言,请提供 English-speaking 语言环境。
Link: Oracle tutorial: Date Time 解释如何使用 java.time
.
我正在尝试更新 google 中的日历事件开始和结束日期时间 spring class.But 我正在获取 DateFormat exception.I 我能够获取特定事件并显示在模态 popup.I 我正在使用 kendo 指令 datepicker.Now 如果用户编辑开始和结束日期,它将以这种格式更改
String start=2/21/2018 8:00 PM
String end:2/22/2018 9:00 PM
现在我正尝试在 class
中像这样设置 google 事件开始和结束日期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.But 如果像这样直接设置
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")));
正在运行 perfectly.Can 谁能告诉我如何将日期 2/21/2018 8:00 解析为这种格式 2018-02-21T08:00:00.000+05:30?
如果你想解析格式的日期:2/21/2018 8:00 PM 你必须将模式更改为:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
有关详细信息,请参阅 javadoc of SimpleDateFormat
我认为您使用的DateTime
class是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();
}
编辑: 我知道您的 date-time 字符串也可能像 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-time classes,Date
和 SimpleDateFormat
早已过时,而后者尤其麻烦。所以我在我的代码中避免使用它们。现代 API 更好用。
如果不是 Asia/Kolkata,请替换为您想要的时区。您可以使用 ZoneId.systemDefault()
作为 JVM 的时区设置,只是要知道该设置可能会被您程序的其他部分或同一 JVM 中的其他程序 运行 更改。由于 AM 和 PM 几乎不用于英语以外的其他语言,请提供 English-speaking 语言环境。
Link: Oracle tutorial: Date Time 解释如何使用 java.time
.