How to set time zone of a java.util.Date?

后端 未结 10 1822
我寻月下人不归
我寻月下人不归 2020-11-22 01:45

I have parsed a java.util.Date from a String but it is setting the local time zone as the time zone of the date object.

The ti

10条回答
  •  太阳男子
    2020-11-22 02:09

    You could also set the timezone at the JVM level

    Date date1 = new Date();
    System.out.println(date1);
    
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    // or pass in a command line arg: -Duser.timezone="UTC"
    
    Date date2 = new Date();
    System.out.println(date2);
    

    output:

    Thu Sep 05 10:11:12 EDT 2013
    Thu Sep 05 14:11:12 UTC 2013
    

提交回复
热议问题