0

I wrote the following code:

public static void main(String args[]) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat("y:M:dd z");
        Date d = dateFormat.parse("2015:7:2 JST");
        System.out.println(d);
    }

It works fine, but the problem is that every time d will have machine`s timeZone. Is there a way to specify "destination" timeZone for SimpleDateFormat?

lucian.marcuta
  • 1,250
  • 1
  • 16
  • 29
  • 1
    A Date doesn't have **any** timezone. Calling `toString()` on a Date returns the date's representation in the default timezone. If you want the date representation in another timezone, use a SimpleDateFormat with that timezone to format the date – JB Nizet Jul 03 '15 at 07:02
  • try using below code (by setting respective timezone) : SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); sdf.setTimeZone(TimeZone.getTimeZone("GMT")); – techGaurdian Jul 03 '15 at 07:04
  • Ok, thanks. The question is now, how can "use a SimpleDateFormat with that timezone to format the date"? For example a user is able "2015:7:2 JST", but i want SimpleDateFormat to convert it to GMT timezone(even though mmachine`s time zone is not GMT). – lucian.marcuta Jul 03 '15 at 07:06
  • http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setTimeZone%28java.util.TimeZone%29 – JB Nizet Jul 03 '15 at 07:07

0 Answers0