Java 如何从纪元时间戳中删除小时、分钟和秒
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19123020/
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
How to remove Hour, Minute, and Second from Epoch timestamp
提问by Churro
I need to write a function that accepts a java.util.Date
and removes the hours, minutes, and milliseconds from it USING JUST MATH (no Date formatters, no Calendar objects, etc.):
我需要编写一个函数,它接受 ajava.util.Date
并使用 JUST MATH(没有日期格式化程序,没有日历对象等)从中删除小时、分钟和毫秒:
private Date getJustDateFrom(Date d) {
//remove hours, minutes, and seconds, then return the date
}
The purpose of this method is to get the date from a millisecond value, without the time.
此方法的目的是从毫秒值中获取日期,而不是时间。
Here's what I have so far:
这是我到目前为止所拥有的:
private Date getJustDateFrom(Date d) {
long milliseconds = d.getTime();
return new Date(milliseconds - (milliseconds%(1000*60*60)));
}
The problem is, this only removes minutes and seconds. I don't know how to remove hours.
问题是,这只会删除分钟和秒。我不知道如何删除小时数。
If I do milliseconds - (milliseconds%(1000*60*60*23))
, then it goes back to 23:00 hrs on the previous day.
如果我这样做milliseconds - (milliseconds%(1000*60*60*23))
,那么它会回到前一天的 23:00 小时。
EDIT:
编辑:
Here's an alternative solution:
这是一个替代解决方案:
public static Date getJustDateFrom(Date d) {
Calendar c = Calendar.getInstance();
c.setTime(d);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
return c.getTime();
}
Will this solution be affected by time zone differences between the client/server sides of my app?
此解决方案是否会受到我的应用程序客户端/服务器端之间时区差异的影响?
采纳答案by Mark Bailey
There are 24 hours in a day. Use milliseconds%(1000*60*60*24).
一天有 24 小时。使用毫秒%(1000*60*60*24)。
回答by Durandal
Simply not possible by your definition.
根据您的定义根本不可能。
A millisecond timestamp represents milliseconds elapsed from a fixed point in time (1970-01-01 00:00:00.000 UTC, if I remember correctly). This timestamp can not be converted into a date + time without specifying the timezone to convert to.
毫秒时间戳表示从固定时间点(1970-01-01 00:00:00.000 UTC,如果我没记错的话)过去的毫秒数。如果不指定要转换到的时区,则无法将此时间戳转换为日期 + 时间。
So you can only round the timestamp to full days in respect to a specific timezone, not in general. So any fiddling with Date.getTime() and not taking into account any timezone is guaranteedto work in only one time zone - the one you hardcoded for.
因此,您只能将时间戳四舍五入到特定时区的全天,而不是一般情况下。因此,任何对 Date.getTime() 的摆弄而不考虑任何时区都保证只能在一个时区中工作 - 您硬编码的那个时区。
Do yourself a favor and use a Calendar.
帮自己一个忙,使用日历。
回答by mnm
You can make use of apache's commons lang DateUtils helper utility class.
您可以使用 apache 的 commons lang DateUtils helper 实用程序类。
For example, if you had the datetime of 28 Mar 2002 13:45:01.231, if you passed with Calendar.HOUR, it would return 28 Mar 2002 13:00:00.000. If this was passed with Calendar.MONTH, it would return 1 Mar 2002 0:00:00.000.
例如,如果您的日期时间为 2002 年 3 月 28 日 13:45:01.231,如果您通过 Calendar.HOUR 传递,它将返回 2002 年 3 月 28 日 13:00:00.000。如果这是通过 Calendar.MONTH 传递的,它将返回 1 Mar 2002 0:00:00.000。
Date newDate = DateUtils.truncate(new Date(1408338000000L), Calendar.DAY_OF_MONTH);
You can download commons lang jar at http://commons.apache.org/proper/commons-lang/
您可以在http://commons.apache.org/proper/commons-lang/下载 commons lang jar