1

Currently a Java application is deployed in multiple countries at site location. The local dates and time of some operations are passed to the application without the timezone.

Have to convert each of these local timestamps to UTC. How can I get the localTimeZone(depending on where the application is running), check this and convert all dates to UTC.

Rehan
  • 929
  • 1
  • 12
  • 29
  • 1
    This question topics the conversion of local time to utc and back. Might be what you need. https://stackoverflow.com/questions/19375357/java-convert-gmt-utc-to-local-time-doesnt-work-as-expected – Akunosh Mar 02 '16 at 12:02
  • Thanks Akunosh..the link helped – Rehan Mar 04 '16 at 07:56

2 Answers2

0

please, do something likewise,

TimeZone currentTimeZone = TimeZone.getDefault();

        DateFormat formatter = DateFormat.getDateTimeInstance(
                DateFormat.DEFAULT,
                DateFormat.DEFAULT,
                Locale.getDefault());
        formatter.setTimeZone(currentTimeZone);

        Date myDate = new Date();
        System.out.println( formatter.format(myDate)); // you Local time

        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println(formatter.format(myDate)); // UTC time
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55
0

I find this answer from here. You can try this, simple and easy and can convert any time zone to any zone

SimpleDateFormat formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");

String dateInString = "22-01-2015 10:15:55 AM";
Date date = formatter.parse(dateInString);
TimeZone tz = TimeZone.getDefault();

// From TimeZone Asia/Singapore
System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName());
System.out.println("TimeZone : " + tz);
System.out.println("Date : " + formatter.format(date));

// To TimeZone America/New_York
SimpleDateFormat sdfAmerica = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
TimeZone tzInAmerica = TimeZone.getTimeZone("America/New_York");
sdfAmerica.setTimeZone(tzInAmerica);

String sDateInAmerica = sdfAmerica.format(date); // Convert to String first
Date dateInAmerica = formatter.parse(sDateInAmerica);

System.out.println("\nTimeZone : " + tzInAmerica.getID() + 
                                  " - " + tzInAmerica.getDisplayName());
System.out.println("TimeZone : " + tzInAmerica);
System.out.println("Date (String) : " + sDateInAmerica);
System.out.println("Date (Object) : " + formatter.format(dateInAmerica));
Emdadul Sawon
  • 5,730
  • 3
  • 45
  • 48