Reputation: 15
I have xml in which two field are Date like below
<CreateDate1>2019-03-07T09:42:20.65737Z</CreateDate1>
<CreateDate2>2019-03-07T00:00:00</CreateDate2>
when I am specifying type String in Pojo for these two, I am able to get the same values as in xml in String format.
But My requirement is to get these two field in Date format.
So I tried specifying Date type in Pojo for these two but i am getting some different format result for these two.
output :
CreateDate1= Thu Mar 07 15:12:20 IST 2019, CreateDate2= Thu Mar 07 00:00:00 IST 2019
expected output in Date format: 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 + "]";
}
Can someone please hepp me on this .. Thanks in advance
Upvotes: 1
Views: 186
Reputation: 79095
Use ZonedDateTime
for createDate1
(because you need time-zone information in it) and LocalDateTime
for createDate2
(because you do not need time-zone or offset information in it).
Given below is how you should parse the date-time string and how you should create a String
to be returned from the toString()
method:
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);
}
}
Output:
2019-03-07T09:42:20.657370Z
Sample [CreateDate1= 2019-03-07T09:42:20.657370Z, CreateDate2= 2019-03-07T00.00.00]
Upvotes: 0
Reputation: 159114
The <CreateDate1>
value 2019-03-07T09:42:20.65737Z
has a time zone offset (the Z
at the end), specifying a +00:00
offset, i.e. the UTC
offset. As such, it parses to Date
exactly and displays by default in the JVM's default time zone, which is IST
in your example, and explains the +05:00
offset in the displayed time-of-day.
The <CreateDate2>
value 2019-03-07T00:00:00
does not have a time zone offset, so it is parsed in the JVM's default time zone, and displays in the default time zone, which explains why the time-of-day is same as input. The actual Date
value varies, depending on the JVM's default time zone.
If you running with Java 8 or later, you should not use Date
for this, since the result varies.
Instead, use Instant
(or OffsetDateTime
or ZonedDateTime
) for CreateDate1
. Instant
requires the input to use Z
as time zone offset. The other two can handle other offsets.
Use LocalDateTime
for CreateDate2
, since that correctly represents a date/time value without a time zone.
If you make these changes, the output will be:
Sample [CreateDate1= 2019-03-07T09:42:20.657370Z, CreateDate2= 2019-03-07T00:00]
Upvotes: 1