9

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;
}
Vicky
  • 1,412
  • 4
  • 18
  • 30
  • 2
    What exactly do you mean by "IST" (Indian standard time; Irish standard time; Israel standard time?); and what is the JVM's default timezone on the server? – Andy Turner Feb 01 '16 at 09:48
  • What is the type of `utcDate`? I assume it is a `java.util.Date`. – Andy Turner Feb 01 '16 at 09:50
  • Your code never actually converts to IST, you literally set sdf to "UTC", and then just parse the formatted date. – Paddez Feb 01 '16 at 09:50
  • Sorry i have update the code now check it i am getting ist time correctly with +5.30 conversion @Paddez and i mean ist as Indian Standard Time – Vicky Feb 01 '16 at 09:53
  • 1
    Try using TimeZone set and get again as suggested in [this answer](http://stackoverflow.com/a/14314481/2250010). – Fanchi Feb 01 '16 at 09:55
  • I am not so sure what you are trying to do. If you are trying to get IST time you should set the timezone to IST. `sdf.setTimeZone(TimeZone.getTimeZone("IST"));` – Helios Feb 01 '16 at 10:04
  • Thanks its worked i am setting the timezone of formatter to IST and the time as indian time perfectly from the server formatter.setTimeZone(TimeZone.getTimeZone("IST")); – Vicky Feb 01 '16 at 10:24
  • @Vicky, as shown in Andreas's answer, use `Asia/Kolkata`. It is more correct than `IST`. – Matt Johnson-Pint Feb 01 '16 at 21:14
  • Ok thanks but is there any difference @MattJohnson – Vicky Feb 05 '16 at 07:56

2 Answers2

6

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);
Andreas
  • 154,647
  • 11
  • 152
  • 247
3

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);
Rakesh Chaudhari
  • 3,310
  • 1
  • 27
  • 25