Converting UTC to IST time in java works in LOCAL, but not in CLOUD SERVER

I work in converting dates in java in that I use the following code snippet to convert UTC time to IST format. It works correctly in the local one when I run it, but when I deploy it on the server, it does not convert it, it displays only the utc time itself. Is there any configuration on the server side. Please help me.

CODE SNIPPET:

DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); String pattern = "dd-MM-yyyy HH:mm:ss"; SimpleDateFormat formatter; formatter = new SimpleDateFormat(pattern); try { String formattedDate = formatter.format(utcDate); Date ISTDate = sdf.parse(formattedDate); String ISTDateString = formatter.format(ISTDate); return ISTDateString; } 
+5
source share
1 answer

Java Date objects are already / always in UTC. The time zone is what applies when formatting text. A Date cannot (should not!) Be in any time zone except UTC.

Thus, the whole concept of converting utcDate to ISTDate is erroneous.
(BTW: Bad name. Java conventions say it should be ISTDate )

Now, if you want the code to return the date as text in the IST time zone, you need to request the following:

 DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); formatter.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); // Or whatever IST is supposed to be return formatter.format(utcDate); 
+3
source

Source: https://habr.com/ru/post/1241982/


All Articles