How do I get a UTC Timestamp from Calendar?

前端 未结 4 574
栀梦
栀梦 2021-02-07 19:55

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条回答
  •  -上瘾入骨i
    2021-02-07 20:21

    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.

提交回复
热议问题