converting/parsing 字符串到日期时间 Java 时丢失日期
Lost date while converting/parsing String to Datetime Java
DateTimeFormatter dateFormatter1 =
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String timestamp = "2021-02-03 23:22:23 +0000 UTC";
DateTime converted1 = dateFormatter1.parseDateTime(timestamp);
Invalid format: "2021-02-03 23:22:23 +0000 UTC" is malformed at " 23:22:23 +0000 UTC"
java.lang.IllegalArgumentException: Invalid format: "2021-02-03 23:22:23 +0000 UTC" is malformed at " 23:22:23 +0000 UTC"
将字符串解析为日期时间时出现异常 Java,有人可以帮忙吗?
尝试了各种格式,例如:
yyyy-MM-dd'T'HH:mm:ss'Z'
yyyy-MM-dd'T'HH:mm:ssZ
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
yyyy-MM-dd HH:mm:ss.SSS
(yyyy-MM-dd'T'HH:mm:ss'Z').withZone(UTC)
您的模式不正确。使用 yyyy-MM-dd HH:mm:ss Z z
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String timestamp = "2021-02-03 23:22:23 +0000 UTC";
DateTimeFormatter fmtInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z z");
DateTime dtSource = fmtInput.parseDateTime(timestamp);
System.out.println(dtSource);
}
}
输出:
2021-02-03T23:22:23.000Z
DateTimeFormatter dateFormatter1 =
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
String timestamp = "2021-02-03 23:22:23 +0000 UTC";
DateTime converted1 = dateFormatter1.parseDateTime(timestamp);
Invalid format: "2021-02-03 23:22:23 +0000 UTC" is malformed at " 23:22:23 +0000 UTC"
java.lang.IllegalArgumentException: Invalid format: "2021-02-03 23:22:23 +0000 UTC" is malformed at " 23:22:23 +0000 UTC"
将字符串解析为日期时间时出现异常 Java,有人可以帮忙吗?
尝试了各种格式,例如:
yyyy-MM-dd'T'HH:mm:ss'Z'
yyyy-MM-dd'T'HH:mm:ssZ
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
yyyy-MM-dd HH:mm:ss.SSS
(yyyy-MM-dd'T'HH:mm:ss'Z').withZone(UTC)
您的模式不正确。使用 yyyy-MM-dd HH:mm:ss Z z
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String timestamp = "2021-02-03 23:22:23 +0000 UTC";
DateTimeFormatter fmtInput = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss Z z");
DateTime dtSource = fmtInput.parseDateTime(timestamp);
System.out.println(dtSource);
}
}
输出:
2021-02-03T23:22:23.000Z