-1

I would like to get the equivalent UTC time for a particular timeZone in Java as a Date Object (not as a string for displaying). Say I need to set the Date property as current US/Pacific time plus 1 hour. Currently if the US/Pacific time is 08.30am then I need the UTC equivalent of 09.30am US/Pacific. How can i do that?

User_007
  • 165
  • 2
  • 15
  • Possible duplicate of [How can I get the current date and time in UTC or GMT in Java?](http://stackoverflow.com/questions/308683/how-can-i-get-the-current-date-and-time-in-utc-or-gmt-in-java) –  Oct 21 '15 at 16:02

1 Answers1

0

Please refer to below code. You may create a method to return Date instance. Hope it helps. Thanks.

DateFormat utcDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    utcDateFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));

    DateFormat pacificFormatter = new SimpleDateFormat("HH:mm");
    pacificFormatter.setTimeZone(TimeZone.getTimeZone("US/Pacific"));
    //add 1 hour to Date
    Calendar pacificCal = Calendar.getInstance();
    pacificCal.add(Calendar.HOUR, 1);
    System.out.println("PACIFIC TIME WITH ADDED HOUR : " +pacificFormatter.format(pacificCal.getTime()));

    String temp = utcDateFormatter.format(pacificCal.getTime());
    System.out.println("GMT DATE TIME: " +temp);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    Date gmtDate = simpleDateFormat.parse(temp);
    //return gmtDate
    System.out.println("GMT DATE: " + gmtDate);
dossani
  • 1,892
  • 3
  • 14
  • 23