在 Java 中将字符串转换为 OffsetDateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44297939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to OffsetDateTime in Java
提问by Shashwat Shekhar Shukla
I am trying to convert a string in OffsetDateTime
but getting below error.
我正在尝试转换一个字符串,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
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
Code : OffsetDateTime.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));
代码 : OffsetDateTime.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));
Expected output: OffsetDateTime object with date 20150101.
预期输出: OffsetDateTime object with date 20150101.
I really appreciate any help you can provide.
我非常感谢您能提供的任何帮助。
Thanks,
谢谢,
采纳答案by Shashwat Shekhar Shukla
Thanks everyone for your reply. Earlier I was using joda datetime (look at below method) to handle date and datetime both but I wanted to use Java8 libraries instead of the external libraries.
谢谢大家的回复。早些时候我使用 joda datetime(看下面的方法)来处理日期和日期时间,但我想使用 Java8 库而不是外部库。
static public DateTime convertStringInDateFormat(String date, String dateFormat){
DateTimeFormatter formatter = DateTimeFormat.forPattern(dateFormat);
return formatter.parseDateTime(date);
}
I was expecting same with OffsetDateTime but got to know we can use ZonedDateTime or OffsetDateTime if we want to work with a date/time in a certain time zone. As I am working on Period and Duration for which LocalDate can help.
我期待与 OffsetDateTime 相同,但我知道如果我们想在某个时区使用日期/时间,我们可以使用 ZonedDateTime 或 OffsetDateTime 。因为我正在研究 LocalDate 可以提供帮助的 Period 和 Duration。
String to DateTime:
字符串到日期时间:
LocalDate date =
LocalDate.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));
LocalDate to desired string format:
LocalDate 到所需的字符串格式:
String dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'";
date.atStartOfDay().format(DateTimeFormatter.ofPattern(dateFormat));
回答by YuVi
回答by Pallavi Sonal
OffsetDateTime
represents a date-time with an offset , for eg.
OffsetDateTime
表示具有偏移量的日期时间,例如。
2007-12-03T10:15:30+01:00
2007-12-03T10:15:30+01:00
The text you are trying to parse does not conform to the requirements of OffsetDateTime.
See https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
您尝试解析的文本不符合OffsetDateTime.
See https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html的要求
The string being parsed neither contains the ZoneOffset nor time. From the string and the pattern of the formatter, it looks like you just need a LocalDate. So, you could use :
被解析的字符串既不包含 ZoneOffset 也不包含时间。从格式化程序的字符串和模式来看,您似乎只需要一个 LocalDate。所以,你可以使用:
LocalDate.parse("20150101", DateTimeFormatter.ofPattern("yyyyMMdd"));