importjava.time.ZonedDateTime;// 导入用于处理带时区的日期时间类importjava.time.format.DateTimeFormatter;// 导入用于格式化日期时间的类importjava.time.ZoneOffset;// 导入ZoneOffset类publicclassTimeZoneDemo{publicstaticvoidmain(String[]args){// 第一步:获取当前的日期时间ZonedDateTimecurrentDateTime=ZonedDateT...
publicclassTimestampToUtcExample{publicstaticvoidmain(String[]args){longtimestamp=1624828800000L;// 指定的时间戳Datedate=newDate(timestamp);SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");sdf.setTimeZone(TimeZone.getTimeZone("UTC"));StringutcTime=sdf.format(date);System.out.pri...
dateToUtc(newDate()); }publicstaticvoiddateToUtc(Date date) { sdfutc.setTimeZone(TimeZone.getTimeZone("UTC"));//sdfutc.setTimeZone(TimeZone.getTimeZone("GMT"));System.out.println("北京时间: " +sdf.format(date)); System.out.println("UTC时间: " +sdfutc.format(date)); } } 运行...
new Date(timestamp)将时间戳转换为Date对象。 SimpleDateFormat用于格式化日期时间,通过setTimeZone(TimeZone.getTimeZone("UTC"))方法将时区设置为UTC。 最后,通过format(date)方法将Date对象格式化为可读的日期时间字符串并输出。这样,你就可以将Java时间戳转换为UTC时间,并将其格式化为一个易于阅读的日期时间字符串...
我使用这两种方法将当地时间转换为 GMT/UTC,反之亦然,这对我来说没有任何问题。 public static Date localToGMT() { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); Date gmt = new Date(sdf.forma...
public static void dateToUtc(Date date) { sdfutc.setTimeZone(TimeZone.getTimeZone("UTC"));// sdfutc.setTimeZone(TimeZone.getTimeZone("GMT"));System.out.println("北京时间: " + sdf.format(date));System.out.println("UTC时间: " + sdfutc.format(date));} } 运⾏结果如下:北京时...
Date utcDate=null;try{ utcDate=sdf.parse(utcTime); }catch(ParseException e) { e.printStackTrace(); } sdf.setTimeZone(TimeZone.getDefault()); Date locatlDate=null; String localTime=sdf.format(utcDate.getTime());try{ locatlDate=sdf.parse(localTime); ...
选择需要将日期:2023-03-31 13:24:51 转换为 UTC零时区格式的日期数据 思路: 采用joda.time 日期处理工具类 代码: @Testpublic void redd111(){System.out.println(toTimeFormatZone0("2023-03-31 13:24:51"));}public String toTimeFormatZone0(String timeString) {if (StringUtils.isBlank(timeString)...
newYorkDateFormat.setTimeZone(newYorkTimeZone); System.out.println("这是北京时间:" + new SimpleDateFormat(patternStr).format(bjDate)); System.out.println("这是纽约时间:" + newYorkDateFormat.format(bjDate)); } 运行程序,输出: 代码语言:txt ...
utcTimeZone=TimeZone.getTimeZone("UTC");// 创建SimpleDateFormat对象,指定输出格式和时区为UTCSimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");sdf.setTimeZone(utcTimeZone);// 格式化时间StringutcTime=sdf.format(date);// 输出转换后的UTC时间System.out.println("UTC时间:"+utcTime)...