Java 日期截止时间信息

2022-01-13 00:00:00 time timestamp date java

我有一个包含日期和时间信息的 Java Date 对象.我想写一个方法,截断时间信息,截断时分秒,所以我只剩下日期了.

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.

示例输入:

2008-01-01 13:15:00

预期输出:

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)

但我遇到了时区问题.

推荐答案

推荐 进行日期/时间操作的方法是使用 Calendar 对象:

The recommended way to do date/time manipulation is to use a Calendar object:

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();

相关文章