0

I am trying to convert the Date received from Server to user specific date format (IST) and returning as Date datatype. Attached the sample code and result as below

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class ServerToClientDateTime {
    public static void main(String[] args) {
        try {
            String clientSpecificFormat = "yyyy-MM-dd HH:mm:ss";
            String clientTimezone1 = "America/New_York";
            String clientTimezone2 = "Asia/Kolkata";
            Date date = new Date();
            System.out.println(new Date());
            SimpleDateFormat dateFormat = new g(clientSpecificFormat);
            TimeZone americaNewyork = TimeZone.getTimeZone(clientTimezone1);
            TimeZone asiaKolkata = TimeZone.getTimeZone(clientTimezone2);
            dateFormat.setTimeZone(americaNewyork);

            System.out.println("\namerica/ newyork:" + dateFormat.format(date));
            System.out.println("Date format :" + dateFormat.parse(dateFormat.format(date)));

            dateFormat.setTimeZone(asiaKolkata);
            System.out.println("\nAsia/ Kolkata:" + dateFormat.format(date));
            System.out.println("Date format :" + dateFormat.parse(dateFormat.format(date)));
        } catch (Exception exp) {
            System.out.println(exp);
        }
    }
    
}

Result for the Program as

Wed Aug 09 11:57:46 IST 2023

america/ newyork:2023-08-09 02:27:46
Date format :Wed Aug 09 11:57:46 IST 2023

Asia/ Kolkata:2023-08-09 11:57:46
Date format :Wed Aug 09 11:57:46 IST 2023

why America/New_York return same as GMT after parsing, how to get the expected IST date format.

Thanks

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Prakash
  • 630
  • 3
  • 10
  • 20
  • 3
    Basically, there's no such thing as a `Date` in a particular time zone. See https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/ (I'd very strongly advise you to use java.time instead.) – Jon Skeet Aug 09 '23 at 06:37
  • 1
    *I am trying to convert the Date received from Server* How is it received and in what form? – g00se Aug 09 '23 at 08:27
  • new Date() provides a Server date in GMT format. – Prakash Aug 09 '23 at 11:10
  • 1
    You should probably start with `Instant.now()` at the server, having ensured the system clock is set to UTC – g00se Aug 09 '23 at 11:15
  • I strongly recommend that you do not use `Date`, `SimpleDateFormat` and `TimeZone`. Those classes were poorly designed and are long outdated. Use `ZonedDateTime`, `ZoneId` and `DateTimeFormatter`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Aug 09 '23 at 11:31
  • 1
    @Prakash It does not since a `java.util.Date` neither can have a format nor be in GMT (GMT is not a format, it’s a time zone or an offset of zero). – Ole V.V. Aug 09 '23 at 11:34
  • 1
    *I am trying to convert the Date received from Server to user specific date format (IST) and returning as Date datatype.* - Return your result as `String`, not `Date` as a `Date` can not hold the information about format and timezone. – Arvind Kumar Avinash Aug 10 '23 at 09:13

0 Answers0