Business case:
A date wield of dataType LocalDate
needs to be converted into epoch
.
private LocalDate someDate;
Problem I am having is, with day light saving in place sometimes date is getting converted to n-1.
Example: Date is 28-Feb-2037
is converted into => 27-Feb-2037
which is incorrect.
Explanation: When we are converting from LocalDate to Epoch and taking the ZoneId as system default, it takes in consideration the daylight savings. For this reason the epoch is decreasing by 1 hour and when UI again converts it to date it becomes n-1 date 23:00:00 hrs
.
My conversion code looks like this
someDate.atStartOfDay(ZoneId.systemDefault()).toEpochSecond() * 1000); //systemDefault = EUROPE/PARIS
How to ignore Daylight-Savings while conversion?
By Ignoring timezone below code will do the job for you
localDate.atStartOfDay().atOffset(UTC).toEpochSecond()
Or Maybe you need this:
TimeZone timeZone = TimeZone.getTimeZone("GMT+1");// Paris timezone without daylight saving
localDate.atStartOfDay(timeZone.toZoneId()).toEpochSecond();
The idea is to use a fixed zone offset to get rid of daylight saving.