将UTC日期转换为毫秒 [英] Convert UTC date into milliseconds
问题描述
我对当前的UTC时间毫秒不感兴趣,也不需要弄乱时区。我的原始日期已经存储为UTC时间戳。
I am not interested in what the current UTC time is in milliseconds, nor do I need to mess with timezones. My original date is already stored as a UTC timestamp.
我在UTC时间存储在数据库中的日期,2012-06-14 05:01:25 。
我对日期时间不感兴趣,只是它的日期部分。因此,在使用Java检索日期并排除小时,分钟和秒后 - 我留下了2012-06-14。
I have a date stored in a database in UTC time, "2012-06-14 05:01:25". I am not interested in the datetime, but just the date portion of the it. So, after retrieving the date in Java, and excluding the hours, minutes, and seconds - I am left with "2012-06-14".
我如何转换这是UTC毫秒?
How can I convert this into UTC milliseconds?
推荐答案
编辑:我错过了忽略一天中的时间部分。它现在存在,但接近结束......
I'd missed the "ignoring the time of day" part. It's now present, but near the end...
最简单的方法可能是使用 SimpleDateFormat
,已适当设置时区:
The simplest approach is probably to use SimpleDateFormat
, having set the time zone appropriately:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = format.parse(text);
long millis = date.getTime();
(设置时区是这里的重要位,否则它将解释为的值本地时区。)
(Setting the time zone is the important bit here, as otherwise it will interpret the value to be in the local time zone.)
或者,如果您正在做的事情比这更简单,请使用Joda Time 这是一个更好的日期/时间API。特别是, SimpleDateFormat
不是线程安全的,而 DateTimeFormatter
是:
Alternatively, if you're doing anything less trivial than this, use Joda Time which is a much better date/time API. In particular, SimpleDateFormat
isn't thread-safe whereas DateTimeFormatter
is:
// This can be reused freely across threads after construction.
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
.withLocale(Locale.US)
.withZoneUTC();
// Option 1
DateTime datetime = formatter.parseDateTime(text);
long millis = dateTime.getMillis();
// Option 2, more direct, but harder to diagnose errors
long millis = formatter.parseMillis(text);
现在到目前为止,我们已经解析了整个caboodle。忽略日期部分的最简单方法就是将其四舍五入 - 毕竟,Java没有观察到闰秒,所以我们可以截断它:
Now so far, we've parsed the whole whole caboodle. The easiest way of ignoring the date part is just to round it off - after all, Java doesn't observe leap seconds, so we can just truncate it:
long millisPerDay = 24L * 60L * 60L * 1000L; // Or use TimeUnit
long dayMillis = (millis / millisPerDay) * millisPerDay;
这将向1970年前进,所以如果你有之前的日期 1970年它将转到当天的结束 - 但我怀疑这不太可能是一个问题。
That will "round towards 1970" so if you have a date before 1970 it will round to the end of the day - but I suspect that's unlikely to be a problem.
使用Joda Time版本你可以只需使用它:
With the Joda Time version you could just use this instead:
DateTime dateTime = formatter.parseDateTime(text);
long millis = dateTime.toLocalDate().getLocalMillis();
我个人不只想带一个子串。即使你实际上对保留小时/分钟/秒感兴趣,我认为解析你已经给出的内容并且然后丢弃信息是合适的。除此之外,它会使您的代码在错误数据下失败,例如
I would personally not go with the idea of just taking a substring. Even though you're not actually interested in preserving the hour/minute/second, I think it's appropriate to parse what you've been given and then throw away information. Aside from anything else, it makes your code fail appropriately with bad data, e.g.
"2012-06-100"
或
"2012-06-14 25:01:25"
表示无论是什么为您提供数据,并且发现它是好的,而不是盲目地继续,只因为前10个字符是可以的。
indicate problems in whatever's supplying you data, and it's good to spot that rather than to continue blindly just because the first 10 characters are okay.
这篇关于将UTC日期转换为毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!