I need to query list of records between the given date range based on the column which was defined as datetime2(7) in MS SQL Server.
From the HTTP request I will be receiving startDate and endDate as below.
http://{host:port}/api/records/date?startDate=**2021-05-31T14:12:44.8020000**&endDate=**2021-05-31T14:12:44.8020000**
In the database, value in lastUpdated column is stored as 2021-05-31 14:12:44.8020000
I am trying to convert the incoming query params which is a String to java.sql.Date in the code as below
@Override
public Page<Entity> getAllRecordsWithDateRange(String startDate, String endDate) {
Page<Entity> recordsWithinDateRange = null;
String time = startDate;
String formatIn = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS";
String formatOut = "yyyy-MM-dd HH:mm:ss.SSSSSS";
SimpleDateFormat in = new SimpleDateFormat(formatIn);
SimpleDateFormat out = new SimpleDateFormat(formatOut);
Date dateIn = null;
try {
dateIn = in.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
String valueOut = out.format(dateIn);
System.out.println(">>> " + valueOut);
Pageable page = PageRequest.of(0,5000);
Date date1= null;
try {
date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").parse(valueOut);
java.util.Date utilDate = date1;
recordsWithinDateRange = repo.getAllRecordsBetweenDates(utilDate,utilDate,page);
} catch (ParseException e) {
e.printStackTrace();
}
return recordsWithinDateRange;
}
Issue I am seeing here is my actual input date is 2021-05-31T14:12:44.8020000 But, after the conversion it is incremented to a different time 2021-05-31 16:26:24.000000. So, query is returning no records from the DB.
Could someone help me to solve this issue? TIA!