无法将 org.apache.avro.util.Utf8 解析为 java.time.ZonedDateTime
Unable to parse org.apache.avro.util.Utf8 to java.time.ZonedDateTime
我试过以下测试。但无法解析日期格式:2020-04-19T19:00:54
为什么我会遇到这个问题?
public ZonedDateTime convert(Utf8 date) {
String datee = ((String) date.toString()).replace("T", " ")
LocalDateTime today = LocalDateTime.parse(datee, formatter)
ZonedDateTime currentISTime = today.atZone(ZoneId.systemDefault())
ZonedDateTime currentETime = currentISTime.withZoneSameInstant(ZoneId.systemDefault())
return ZonedDateTime.parse(formatter.format(currentETime), formatter)
}
String datee = ((String) date.toString()).replace("T", " ")
try{
var zoned = ZonedDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(datee.toString()))
return zoned.withZoneSameInstant(ZoneId.systemDefault())
} catch (DateTimeException e) {
//no time zone information -> parse as LocalDate
return ZonedDateTime.parse(datee.toString())
}
异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-04-19 19:00:54' could not be parsed at index 10 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:583)
您不需要 DateTimeFormatter
来解析您的日期时间字符串
您不需要 DateTimeFormatter
来解析您的日期时间字符串,因为您的日期时间字符串已经是 ISO 8601 格式。
现代日期时间 API 基于 ISO 8601 并且不需要明确使用 DateTimeFormatter
对象,只要日期时间字符串符合 ISO 8601 标准.
演示:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
String strDateTime = "2020-04-19T19:00:54";
LocalDateTime ldt = LocalDateTime.parse(strDateTime);
System.out.println(ldt);
// Convert to ZonedDateTime using JVM's timezone
ZonedDateTime zdtSystem = ldt.atZone(ZoneId.systemDefault());
System.out.println(zdtSystem);
// Convert the ZonedDateTime to some other timezone e.g. Asia/Kolkata
ZonedDateTime zdtIndia = zdtSystem.withZoneSameInstant(ZoneId.of("Asia/Kolkata"));
System.out.println(zdtIndia);
}
}
我的系统在 Europe/London 时区的输出:
2020-04-19T19:00:54
2020-04-19T19:00:54+01:00[Europe/London]
2020-04-19T23:30:54+05:30[Asia/Kolkata]
了解有关 modern Date-Time API* from Trail: Date Time 的更多信息。
* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and 。
我试过以下测试。但无法解析日期格式:2020-04-19T19:00:54
为什么我会遇到这个问题?
public ZonedDateTime convert(Utf8 date) {
String datee = ((String) date.toString()).replace("T", " ")
LocalDateTime today = LocalDateTime.parse(datee, formatter)
ZonedDateTime currentISTime = today.atZone(ZoneId.systemDefault())
ZonedDateTime currentETime = currentISTime.withZoneSameInstant(ZoneId.systemDefault())
return ZonedDateTime.parse(formatter.format(currentETime), formatter)
}
String datee = ((String) date.toString()).replace("T", " ")
try{
var zoned = ZonedDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(datee.toString()))
return zoned.withZoneSameInstant(ZoneId.systemDefault())
} catch (DateTimeException e) {
//no time zone information -> parse as LocalDate
return ZonedDateTime.parse(datee.toString())
}
异常:
Exception in thread "main" java.time.format.DateTimeParseException: Text '2020-04-19 19:00:54' could not be parsed at index 10 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2046)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1948)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:598)
at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:583)
您不需要 DateTimeFormatter
来解析您的日期时间字符串
您不需要 DateTimeFormatter
来解析您的日期时间字符串,因为您的日期时间字符串已经是 ISO 8601 格式。
现代日期时间 API 基于 ISO 8601 并且不需要明确使用 DateTimeFormatter
对象,只要日期时间字符串符合 ISO 8601 标准.
演示:
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
String strDateTime = "2020-04-19T19:00:54";
LocalDateTime ldt = LocalDateTime.parse(strDateTime);
System.out.println(ldt);
// Convert to ZonedDateTime using JVM's timezone
ZonedDateTime zdtSystem = ldt.atZone(ZoneId.systemDefault());
System.out.println(zdtSystem);
// Convert the ZonedDateTime to some other timezone e.g. Asia/Kolkata
ZonedDateTime zdtIndia = zdtSystem.withZoneSameInstant(ZoneId.of("Asia/Kolkata"));
System.out.println(zdtIndia);
}
}
我的系统在 Europe/London 时区的输出:
2020-04-19T19:00:54
2020-04-19T19:00:54+01:00[Europe/London]
2020-04-19T23:30:54+05:30[Asia/Kolkata]
了解有关 modern Date-Time API* from Trail: Date Time 的更多信息。
* 无论出于何种原因,如果您必须坚持Java 6 或Java 7,您可以使用ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and