I have the local date in json input in String format dd-MM-yyyy HH:mm:ss
format [let it be any format no issue], have a separate field timezone in header [eg : CST , CDT etc]. In java when i try to create a date with this value and timezone it is getting created with my system [jvm running system] timezone.
I need to persist the exact date and time from input with the input timezone. How can I do it ?
I even tried with putting date object with @DateTimeFormat
in dto instead of string but that did not work.
I want a method like this to get the local date from the input date string and timezone.
public Date getLocalDate(String dateString, String localTimeZone) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss a");
formatter.setTimeZone(TimeZone.getTimeZone(localTimeZone));
return formatter.parse(dateString);
}
Input : dateString : "20-04-2019 20:15:00 AM" localTimeZone : "CDT" .
Expected Output : Date object with value 20-04-2019 20:15:00 AM CDT .
Actual Output : same date with IST-CDT hour difference with IST timezone
There is a misunderstanding about what a Date
object really is. It is just a number of milliseconds since '01-01-1970 00:00:00 UTC'
. Said differently it is always an UTC date/time and it is implicitely converted in your local time zone by the default formatter when you try to print it.
If you want to store both the instant (ie the UTC date/time) and a time zone, you will have to use a Calendar
object. But anyway, if you want to store both the time and the zone in Oracle, you should directly store a string, or a date and a string (for the timezone), because AFAIK, Oracle DATE fields have no notion of a time zone.