简体   繁体   中英

How to convert java.sql.Timestamp to UTC-based string?

When you call .toString on a java.sql.Timestamp instance, you get back a formatted string in your local time zone. I want a formatted string based off of UTC, like the one that I see in the representation. How can I get the one from the representaion?

user> (def timestamp (doto (java.sql.Timestamp. 1366156869000) (.setNanos 42)))
#'user/timestamp
user> timestamp
#inst "2013-04-17T00:01:09.000000042-00:00"
user> (str timestamp)
"2013-04-16 20:01:09.000000042"

I know that I could use a DateFormat , but presumably this is a common issue so I'm wondering if there's something else?

Read this: Converting local timestamp to UTC timestamp in Java

Or you can also Joda time to convert local time to UTC which is probably a better way to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM