Java TimeZones: how to get UTC time values based on Console settings?
For example, when I extract from a database a Date() variable, it contains a date with "+04:00" shift. 开发者_StackOverflow中文版According to my locale's settings. So, UTC time is "ourTime" MINUS 4 hours.
Then, a user tells where he/she is from. Now, we need to save his/her shift. LET'S SAY it is -01:00. And finally, this user tells us where's he/she is heading, let's say this place has a time shift of +05:00.How do we save this town's or place's time shifts in the DB so that according to where he/she's from we could calculate the time difference with our current locale? Also, date save light things, how do we work with them?
You don't.
As long as you're using java.util.Date
objects (or java.sql.Date
, or java.sql.Timestamp
) the database will contain UTC time, that's what java.util.Date
holds internally.
You need to format and/or parse these dates according to local timezone. Take a look at SimpleDateFormat for more information. Of course, if you have dates in the database that you manually parsed, then you may be SOL.
精彩评论