Get Date Object In UTC format in Java

后端 未结 5 1949
不思量自难忘°
不思量自难忘° 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

    A Date doesn't have any time zone. What you're seeing is only the formatting of the date by the Date.toString() method, which uses your local timezone, always, to transform the timezone-agnostic date into a String that you can understand.

    If you want to display the timezone-agnostic date as a string using the UTC timezone, then use a SimpleDateFormat with the UTC timezone (as you're already doing in your question).

    In other terms, the timezone is not a property of the date. It's a property of the format used to transform the date into a string.

提交回复
热议问题