BAEL-3298 Updating Date to LocalDateTime to avoid timezone related issues

This commit is contained in:
Shubhra 2020-03-13 00:06:57 +05:30
parent 7703582c8c
commit 65c76344c7
10 changed files with 50 additions and 30 deletions

View File

@ -1,8 +1,10 @@
package org.baeldung.batch.model;
import java.util.Date;
import org.baeldung.batch.service.adapter.LocalDateTimeAdapter;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.time.LocalDateTime;
@SuppressWarnings("restriction")
@XmlRootElement(name = "transactionRecord")
@ -11,7 +13,7 @@ public class Transaction {
private int userId;
private int age;
private String postCode;
private Date transactionDate;
private LocalDateTime transactionDate;
private double amount;
/* getters and setters for the attributes */
@ -32,11 +34,12 @@ public class Transaction {
this.userId = userId;
}
public Date getTransactionDate() {
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
public LocalDateTime getTransactionDate() {
return transactionDate;
}
public void setTransactionDate(Date transactionDate) {
public void setTransactionDate(LocalDateTime transactionDate) {
this.transactionDate = transactionDate;
}

View File

@ -1,18 +1,19 @@
package org.baeldung.batch.service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.baeldung.batch.model.Transaction;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class RecordFieldSetMapper implements FieldSetMapper<Transaction> {
public Transaction mapFieldSet(FieldSet fieldSet) throws BindException {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyy");
Transaction transaction = new Transaction();
// you can either use the indices or custom names
// I personally prefer the custom names easy for debugging and
@ -20,13 +21,10 @@ public class RecordFieldSetMapper implements FieldSetMapper<Transaction> {
transaction.setUsername(fieldSet.readString("username"));
transaction.setUserId(fieldSet.readInt("userid"));
transaction.setAmount(fieldSet.readDouble(3));
// Converting the date
String dateString = fieldSet.readString(2);
try {
transaction.setTransactionDate(dateFormat.parse(dateString));
} catch (ParseException e) {
e.printStackTrace();
}
transaction.setTransactionDate(LocalDate.parse(dateString, formatter).atStartOfDay());
return transaction;

View File

@ -0,0 +1,19 @@
package org.baeldung.batch.service.adapter;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class LocalDateTimeAdapter extends XmlAdapter<String, LocalDateTime> {
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_FORMAT);
public LocalDateTime unmarshal(String v) throws Exception {
return LocalDateTime.parse(v, DATE_TIME_FORMATTER);
}
public String marshal(LocalDateTime v) throws Exception {
return DATE_TIME_FORMATTER.format(v);
}
}

View File

@ -2,19 +2,19 @@
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<transactionDate>2015-10-31 00:00:00</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<transactionDate>2015-12-03 00:00:00</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<transactionDate>2015-02-02 00:00:00</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>

View File

@ -2,19 +2,19 @@
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<transactionDate>2015-10-31 00:00:00</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<transactionDate>2015-12-03 00:00:00</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<transactionDate>2015-02-02 00:00:00</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>

View File

@ -2,19 +2,19 @@
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<transactionDate>2015-10-31 00:00:00</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<transactionDate>2015-12-03 00:00:00</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<transactionDate>2015-02-02 00:00:00</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>

View File

@ -2,19 +2,19 @@
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<transactionDate>2015-10-31 00:00:00</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<transactionDate>2015-12-03 00:00:00</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<transactionDate>2015-02-02 00:00:00</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>

View File

@ -2,19 +2,19 @@
<transactionRecord>
<transactionRecord>
<amount>10000.0</amount>
<transactionDate>2015-10-31T00:00:00+05:30</transactionDate>
<transactionDate>2015-10-31 00:00:00</transactionDate>
<userId>1234</userId>
<username>devendra</username>
</transactionRecord>
<transactionRecord>
<amount>12321.0</amount>
<transactionDate>2015-12-03T00:00:00+05:30</transactionDate>
<transactionDate>2015-12-03 00:00:00</transactionDate>
<userId>2134</userId>
<username>john</username>
</transactionRecord>
<transactionRecord>
<amount>23411.0</amount>
<transactionDate>2015-02-02T00:00:00+05:30</transactionDate>
<transactionDate>2015-02-02 00:00:00</transactionDate>
<userId>2134</userId>
<username>robin</username>
</transactionRecord>

View File

@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><amount>10000.0</amount><transactionDate>2015-10-31T00:00:00+05:30</transactionDate><userId>1234</userId><username>devendra</username></transactionRecord><transactionRecord><amount>12321.0</amount><transactionDate>2015-12-03T00:00:00+05:30</transactionDate><userId>2134</userId><username>john</username></transactionRecord><transactionRecord><amount>23411.0</amount><transactionDate>2015-02-02T00:00:00+05:30</transactionDate><userId>2134</userId><username>robin</username></transactionRecord></transactionRecord>
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><amount>10000.0</amount><transactionDate>2015-10-31 00:00:00</transactionDate><userId>1234</userId><username>devendra</username></transactionRecord><transactionRecord><amount>12321.0</amount><transactionDate>2015-12-03 00:00:00</transactionDate><userId>2134</userId><username>john</username></transactionRecord><transactionRecord><amount>23411.0</amount><transactionDate>2015-02-02 00:00:00</transactionDate><userId>2134</userId><username>robin</username></transactionRecord></transactionRecord>

View File

@ -1 +1 @@
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><age>10</age><amount>10000.0</amount><postCode>430222</postCode><transactionDate>2015-10-31T00:00:00+05:30</transactionDate><userId>1234</userId><username>sammy</username></transactionRecord><transactionRecord><age>10</age><amount>12321.0</amount><postCode>430222</postCode><transactionDate>2015-12-03T00:00:00+05:30</transactionDate><userId>2134</userId><username>john</username></transactionRecord></transactionRecord>
<?xml version="1.0" encoding="UTF-8"?><transactionRecord><transactionRecord><age>10</age><amount>10000.0</amount><postCode>430222</postCode><transactionDate>2015-10-31 00:00:00</transactionDate><userId>1234</userId><username>sammy</username></transactionRecord><transactionRecord><age>10</age><amount>12321.0</amount><postCode>430222</postCode><transactionDate>2015-12-03 00:00:00</transactionDate><userId>2134</userId><username>john</username></transactionRecord></transactionRecord>