读取 xml 值时转换日期
date convert while xml value read
我有 xml 其中两个字段是 Date 如下所示
<CreateDate1>2019-03-07T09:42:20.65737Z</CreateDate1>
<CreateDate2>2019-03-07T00:00:00</CreateDate2>
当我在 Pojo 中为这两个指定字符串类型时,我能够以字符串格式获得与 xml 中相同的值。
但我的要求是获取日期格式的这两个字段。
所以我尝试在 Pojo 中为这两个指定日期类型,但我得到的是这两个不同的格式结果。
输出:
CreateDate1= Thu Mar 07 15:12:20 IST 2019, CreateDate2= Thu Mar 07 00:00:00 IST 2019
日期格式的预期输出:CreateDate1= 2019-03-07T09:42:20.65737Z, CreateDate2= 2019-03-07T00:00:00
private Date CreateDate1;
private Date CreateDate2;
private Department department;
public Date getCreateDate1() {
return CreateDate1;
}
public Date getCreateDate2() {
return CreateDate2;
}
@Override
public String toString() {
return "Sample [CreateDate1= " + CreateDate1 + ", CreateDate2= " + CreateDate2 + "]";
}
有人可以帮我解决这个问题吗..在此先感谢
对createDate1
使用ZonedDateTime
(因为你需要其中的时区信息),对createDate2
使用LocalDateTime
(因为你不需要时区或偏移量信息在里面)。
下面给出了您应该如何解析日期时间字符串以及您应该如何创建要从 toString()
方法返回的 String
:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Parse the date-time string from your XML as follows. You can do it in
// the constructor(s) or setters
// --------------------------------For createDate1-----------------------------
String dateTimeStr1 = "2019-03-07T09:42:20.65737Z";
ZonedDateTime createDate1 = ZonedDateTime.parse(dateTimeStr1);
System.out.println(createDate1);
// --------------------------------For createDate2-----------------------------
// Define format for parsing
DateTimeFormatter parseFormat = DateTimeFormatter.ofPattern("uuuu-M-d'T'H.m.s");
String dateTimeStr2 = "2019-03-07T00.00.00";
LocalDateTime createDate2 = LocalDateTime.parse(dateTimeStr2, parseFormat);
// -----Create a string to be returned from the `toString()` method as follows------
// Define format for printing
DateTimeFormatter printFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH.mm.ss");
String toString = "Sample [CreateDate1= " + createDate1.toString() + ", CreateDate2= "
+ createDate2.format(printFormat) + "]";
System.out.println(toString);
}
}
输出:
2019-03-07T09:42:20.657370Z
Sample [CreateDate1= 2019-03-07T09:42:20.657370Z, CreateDate2= 2019-03-07T00.00.00]
<CreateDate1>
值2019-03-07T09:42:20.65737Z
有时区偏移(最后的Z
),指定+00:00
偏移,即UTC
抵消。因此,它准确地解析为 Date
并默认显示在 JVM 的默认时区中,在您的示例中为 IST
,并解释了显示时间中的 +05:00
偏移量天.
<CreateDate2>
值2019-03-07T00:00:00
没有时区偏移,所以在JVM的默认时区解析,并显示在默认时区,这就解释了为什么时间- of-day 与输入相同。实际 Date
值会有所不同,具体取决于 JVM 的默认时区。
如果你 运行 Java 8 或更高版本,你不应该为此使用 Date
,因为结果会有所不同。
相反,对 CreateDate1
使用 Instant
(or OffsetDateTime
or ZonedDateTime
)。 Instant
要求输入使用 Z
作为时区偏移量。另外两个可以处理其他偏移量。
将 LocalDateTime
用于 CreateDate2
,因为它正确地表示没有时区的 date/time 值。
如果进行这些更改,输出将是:
Sample [CreateDate1= 2019-03-07T09:42:20.657370Z, CreateDate2= 2019-03-07T00:00]
我有 xml 其中两个字段是 Date 如下所示
<CreateDate1>2019-03-07T09:42:20.65737Z</CreateDate1>
<CreateDate2>2019-03-07T00:00:00</CreateDate2>
当我在 Pojo 中为这两个指定字符串类型时,我能够以字符串格式获得与 xml 中相同的值。
但我的要求是获取日期格式的这两个字段。
所以我尝试在 Pojo 中为这两个指定日期类型,但我得到的是这两个不同的格式结果。
输出:
CreateDate1= Thu Mar 07 15:12:20 IST 2019, CreateDate2= Thu Mar 07 00:00:00 IST 2019
日期格式的预期输出:CreateDate1= 2019-03-07T09:42:20.65737Z, CreateDate2= 2019-03-07T00:00:00
private Date CreateDate1;
private Date CreateDate2;
private Department department;
public Date getCreateDate1() {
return CreateDate1;
}
public Date getCreateDate2() {
return CreateDate2;
}
@Override
public String toString() {
return "Sample [CreateDate1= " + CreateDate1 + ", CreateDate2= " + CreateDate2 + "]";
}
有人可以帮我解决这个问题吗..在此先感谢
对createDate1
使用ZonedDateTime
(因为你需要其中的时区信息),对createDate2
使用LocalDateTime
(因为你不需要时区或偏移量信息在里面)。
下面给出了您应该如何解析日期时间字符串以及您应该如何创建要从 toString()
方法返回的 String
:
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
// Parse the date-time string from your XML as follows. You can do it in
// the constructor(s) or setters
// --------------------------------For createDate1-----------------------------
String dateTimeStr1 = "2019-03-07T09:42:20.65737Z";
ZonedDateTime createDate1 = ZonedDateTime.parse(dateTimeStr1);
System.out.println(createDate1);
// --------------------------------For createDate2-----------------------------
// Define format for parsing
DateTimeFormatter parseFormat = DateTimeFormatter.ofPattern("uuuu-M-d'T'H.m.s");
String dateTimeStr2 = "2019-03-07T00.00.00";
LocalDateTime createDate2 = LocalDateTime.parse(dateTimeStr2, parseFormat);
// -----Create a string to be returned from the `toString()` method as follows------
// Define format for printing
DateTimeFormatter printFormat = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH.mm.ss");
String toString = "Sample [CreateDate1= " + createDate1.toString() + ", CreateDate2= "
+ createDate2.format(printFormat) + "]";
System.out.println(toString);
}
}
输出:
2019-03-07T09:42:20.657370Z
Sample [CreateDate1= 2019-03-07T09:42:20.657370Z, CreateDate2= 2019-03-07T00.00.00]
<CreateDate1>
值2019-03-07T09:42:20.65737Z
有时区偏移(最后的Z
),指定+00:00
偏移,即UTC
抵消。因此,它准确地解析为 Date
并默认显示在 JVM 的默认时区中,在您的示例中为 IST
,并解释了显示时间中的 +05:00
偏移量天.
<CreateDate2>
值2019-03-07T00:00:00
没有时区偏移,所以在JVM的默认时区解析,并显示在默认时区,这就解释了为什么时间- of-day 与输入相同。实际 Date
值会有所不同,具体取决于 JVM 的默认时区。
如果你 运行 Java 8 或更高版本,你不应该为此使用 Date
,因为结果会有所不同。
相反,对 CreateDate1
使用 Instant
(or OffsetDateTime
or ZonedDateTime
)。 Instant
要求输入使用 Z
作为时区偏移量。另外两个可以处理其他偏移量。
将 LocalDateTime
用于 CreateDate2
,因为它正确地表示没有时区的 date/time 值。
如果进行这些更改,输出将是:
Sample [CreateDate1= 2019-03-07T09:42:20.657370Z, CreateDate2= 2019-03-07T00:00]