Java日期截止时间信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1908387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Date cut off time information
提问by Marco
I have a Java Date object containing date and time information. I want to write a method that cuts off the time information, truncates the hours-minutes-seconds, so I only have the date left.
我有一个包含日期和时间信息的 Java Date 对象。我想写一个截断时间信息的方法,截断时分秒,所以我只剩下日期了。
Example input:
示例输入:
2008-01-01 13:15:00
Expected output:
预期输出:
2008-01-01 00:00:00
Do you have a tip? I tried doing something like this:
你有窍门吗?我尝试做这样的事情:
(timestamp / (24 * 60 * 60 * 1000)) * (24 * 60 * 60 * 1000)
but I ran into problems with the timezone.
但我遇到了时区问题。
采纳答案by cletus
The recommendedway to do date/time manipulation is to use a Calendar
object:
进行日期/时间操作的推荐方法是使用Calendar
对象:
Calendar cal = Calendar.getInstance(); // locale-specific
cal.setTime(dateObject);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
long time = cal.getTimeInMillis();
回答by Michael Borgwardt
回答by jitter
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
date = cal.getTime();
回答by Brian Agnew
Have you looked at Joda? It's a mucheasier and more intuitive way to work with dates and times. For instance you can convert trivially between (say) LocalDateTimeand LocalDateobjects.
你看过乔达吗?这是处理日期和时间的一种更简单、更直观的方式。例如,您可以在(例如)LocalDateTime和LocalDate对象之间进行简单的转换。
e.g. (to illustrate the API)
例如(以说明 API)
LocalDate date = new LocalDateTime(milliseconds).toLocalDate()
Additionally it solves some thread-safety issues with date/time formatters and is to be strongly recommended for working with any date/time issues in Java.
此外,它还解决了日期/时间格式化程序的一些线程安全问题,强烈建议用于处理 Java 中的任何日期/时间问题。
回答by A_M
回答by Sunil Manheri
From java.util.Date JavaDocs:
从 java.util.Date JavaDocs:
The class Date represents a specific instant in time, with millisecond precision
类 Date 表示特定的时刻,精度为毫秒
and from the java.sql.Date JavaDocs:
和来自 java.sql.Date JavaDocs:
To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
为了符合 SQL DATE 的定义,java.sql.Date 实例包装的毫秒值必须通过在实例关联的特定时区中将小时、分钟、秒和毫秒设置为零来“标准化” .
So, the best approach is to use java.sql.Date if you are not in need of the time part
因此,如果您不需要时间部分,最好的方法是使用 java.sql.Date
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
and the output is:
输出是:
java.util.Date : Thu Apr 26 16:22:53 PST 2012
java.sql.Date : 2012-04-26
回答by Byron Marambio Troncoso
Use DateUtils from Apache, with truncate, like this:
使用来自 Apache 的 DateUtils,并带有 truncate,如下所示:
DateUtils.truncate(Calendar.getInstance().getTime(), Calendar.DATE);
回答by Gan Quan
For timestamps:
对于时间戳:
timestamp -= timestamp % (24 * 60 * 60 * 1000)
回答by m190
Just clear()
redundant fields.
只是clear()
多余的字段。
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
Date truncatedDate = calendar.getTime();
From Java 8 a better option is to use truncatedTo
method of LocalDateTime
, e.g.:
从 Java 8 开始,更好的选择是使用 的truncatedTo
方法LocalDateTime
,例如:
LocalDateTime.now().truncatedTo(ChronoUnit.DAYS)
回答by h7r
With Joda you can easily get the expected date.
使用 Joda,您可以轻松获得预期日期。
As of version 2.7 (maybe since some previous version greater than 2.2), as a commenter notes, toDateMidnight
has been deprecated in favor or the aptly named withTimeAtStartOfDay()
, making the convenient
从 2.7 版开始(也许是因为某个以前的版本大于 2.2),正如评论者所指出的,toDateMidnight
已被弃用,取而代之的是 或恰当命名的withTimeAtStartOfDay()
,从而方便
DateTime.now().withTimeAtStartOfDay()
possible.
可能的。
Benefit added of a way nicer API.
添加了更好的 API 的好处。
With older versions, you can do
使用旧版本,您可以这样做
new DateTime(new Date()).toDateMidnight().toDate()