We have a resource based service which takes a date and a time as parameters to fetch the data from the backend.
The data is stored in the database in UTC format and the server is also configured withe UTC timezone.
When the request is received from the client I want to convert the date and time to UTC format date and time and then query the database accordingly.
In order to do the conversion, I have the below code which converts from a given zoneId to the UTC format.
public ZonedDateTime convertDateBetweenTimeZones(LocalDateTime sourceDateTime,String sourceZone,String targetZone){
return sourceDateTime.atZone( ZoneId.of(sourceZone)).withZoneSameInstant( ZoneId.of(targetZone));
}
I invoke the above method as below,
ZonedDateTime zonedDateTime=convertDateBetweenTimeZones(LocalDateTime.now(),"Asia/Calcutta","UTC");
Once I can get a zonedDateTime object I think I can pass it the database and query it accordingly.
My question is, in my case, I have passed "Asia/Calcutta" as the zoneID of the source timezone. But, the source timezone can be anything, "America/New_York" or even "Asia/Singapore".
So, in that case, I am not sure how I can dynamically get the zoneId from the request received from the client so that I can convert it accordingly.
Can anyone help me on how can I get the zoneId or alternate way to handle my scenario.
Thanks,
JavaUser