How do I get a UTC Timestamp from Calendar?

前端 未结 4 936
慢半拍i
慢半拍i 2021-02-07 19:56

In Java when I use Calendar.getInstance(); I get a Calendar object for the current Timezone. But java.sql.Timestamp is usually stored in U

4条回答
  •  生来不讨喜
    2021-02-07 20:05

    Your code is already doing exactly what you want. Timestamp (as well as Date) does not have timezone information and should always contain a GMT timestamp (which ist what Calendar.getTimeInMillis() returns).

    The reson why you see local time printed is that the DateFormat factory methods as well as Timestamp.toString() implicitly use the system timezone.

提交回复
热议问题