Home  >  Q&A  >  body text

Java:请教两种获取UTC时间的方式有什么不同?

方式一:

Calendar calendar = Calendar.getInstance();
int offset = calendar.get(Calendar.ZONE_OFFSET); 
calendar.add(Calendar.MILLISECOND, -offset);
Date date = calendar.getTime(); 

方式二:

Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
Date date = calendar.getTime();
PHP中文网PHP中文网2741 days ago204

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-18 09:21:46

    The second one gets the time in GMT time zone, and the first one gets the time before the current time zone. Use Date.format() to print and see the effect.

    reply
    0
  • Cancelreply