Get Date Object In UTC format in Java

后端 未结 5 1951
不思量自难忘°
不思量自难忘° 2020-12-17 07:36

I have written following code. I want to get Date object in UTC format.

I am able to get expected date string in UTC using SimpleDateFormat. But using

5条回答
  •  暖寄归人
    2020-12-17 08:29

    You can subtract the time zone difference from now.

    final Calendar calendar  = Calendar.getInstance();
    final int      utcOffset = calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET);
    final long     tempDate  = new Date().getTime();
    
    return new Date(tempDate - utcOffset);
    

提交回复
热议问题