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

后端 未结 10 2019
我寻月下人不归
我寻月下人不归 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:00

    Convert the Date to String and do it with SimpleDateFormat.

        SimpleDateFormat readFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        readFormat.setTimeZone(TimeZone.getTimeZone("GMT" + timezoneOffset));
        String dateStr = readFormat.format(date);
        SimpleDateFormat writeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date date = writeFormat.parse(dateStr);
    

提交回复
热议问题