首页 > 解决方案 > java format timestamps with "Z" instead of "+"

问题描述

Using java I try to format the current date with the timezone using SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:SSSZ");
sdf.format(new Date());

This give me as results :

   2021-04-28T13:45:52:308+0300

I want to get the timezone format with the "Z" instead of "+"

wanted results : "2021-04-28T13:45:52:308Z03:00"

I writed the date output in a file log that will be parsed by telegraf plugin writed in Go language that expect date with time zone with the following format : json_time_format = "2006-01-02T15:04:05Z07:00"

Is there a pattern allows that ?

标签: javajava-8jodatime

解决方案


Z is for "Zulu time" or zero hour offset, i.e. UTC +0:00

It's not correct to use it if you're not in that timezone. How would you know whether you're before or after the meridian if you replace it with Z? Given Z03:00 do you parse it as +03:00 or -03:00?


推荐阅读