Sha*_*kla 3 java string datetime date java-8
我正在尝试转换字符串,OffsetDateTime但低于错误.
java.time.format.DateTimeParseException: Text '20150101' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2015-01-01 of type java.time.format.Parsed
代码: OffsetDateTime.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));
预期产量: OffsetDateTime object with date 20150101.
我非常感谢您提供的任何帮助.
谢谢,
OffsetDateTime 表示具有偏移的日期时间,例如.
2007-12-03T10:15:30 + 01:00
您尝试解析的文本不符合OffsetDateTime.
请参阅https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html的要求
被解析的字符串既不包含ZoneOffset也不包含时间.从字符串和格式化程序的模式,看起来你只需要一个LocalDate.所以,你可以使用:
LocalDate.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));
Run Code Online (Sandbox Code Playgroud)
谢谢大家的回复。早些时候,我使用 joda datetime (请看下面的方法)来处理日期和日期时间,但我想使用 Java8 库而不是外部库。
static public DateTime convertStringInDateFormat(String date, String dateFormat){
DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat);
return formatter.parseDateTime(date);
}
Run Code Online (Sandbox Code Playgroud)
我期待与 OffsetDateTime 相同,但知道如果我们想处理特定时区的日期/时间,我们可以使用 ZonedDateTime 或 OffsetDateTime。因为我正在研究 LocalDate 可以提供帮助的期间和持续时间。
字符串到日期时间:
LocalDate date =
LocalDate.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));
Run Code Online (Sandbox Code Playgroud)
LocalDate 为所需的字符串格式:
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
date.atStartOfDay().format(DateTimeFormatter.ofPattern(dateFormat));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15454 次 |
| 最近记录: |