Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of UTC to IST time in java is working in LOCAL but not in CLOUD SERVER

I am working in date conversion in java in that i am using following code snippet to convert the UTC time to IST format.It is working properly in the local when i run it but when i deploy it in server its not converting , its displaying only the utc time itself.Is there any configuaration is needed in server side.Please help me out.

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;
}
like image 220
Vicky Avatar asked Feb 01 '16 09:02

Vicky


People also ask

How do you convert UTC to normal time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.). Note The date also follows UTC format.

Is Java date always UTC?

java. util. Date has no specific time zone, although its value is most commonly thought of in relation to UTC.

What is UTC timezone in Java?

UTC stands for Co-ordinated Universal Time. It is time standard and is commonly used across the world. All timezones are computed comparatively with UTC as offset.

What is UTC date format in Java?

Parse String to ZonedDateTime in UTC Date time with full zone information can be represented in the following formats. dd/MM/uuuu'T'HH:mm:ss:SSSXXXXX pattern. e.g. "03/08/2019T16:20:17:717+05:30" . MM/dd/yyyy'T'HH:mm:ss:SSS z pattern.


2 Answers

Java Date objects are already/always in UTC. Time Zone is something that is applied when formatting to text. A Date cannot (should not!) be in any time zone other than UTC.

So, the entire concept of converting utcDate to ISTDate is flawed.
(BTW: Bad name. Java conventions says it should be istDate)

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

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);
like image 159
Andreas Avatar answered Sep 16 '22 17:09

Andreas


Using Java 8 New API,

Instant s = Instant.parse("2019-09-28T18:12:17Z");
        ZoneId.of("Asia/Kolkata");
        LocalDateTime l = LocalDateTime.ofInstant(s, ZoneId.of("Asia/Kolkata"));
        System.out.println(l);
like image 24
Rakesh Chaudhari Avatar answered Sep 16 '22 17:09

Rakesh Chaudhari