Convert Java Date to UTC String

前端 未结 7 500
予麋鹿
予麋鹿 2020-11-29 06:09

The java.util.Date toString() method displays the date in the local time zone.

There are several common scenarios where we want the data to be printed in

7条回答
  •  盖世英雄少女心
    2020-11-29 06:12

    The following simplified code, based on the accepted answer above, worked for me:

    public class GetSync {
        public static String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS zzz";
        private static final TimeZone utc = TimeZone.getTimeZone("UTC");
        private static final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
        static {
            isoFormatter.setTimeZone(utc);
        }
    
        public static String now() {
            return isoFormatter.format(new Date()).toString();
        }
    }
    

    I hope this helps somebody.

提交回复
热议问题