在 Java 中获取今天的日期 - 我已经尝试过常规方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/829510/
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
Getting today's date in java - I've tried the regular ways
提问by Yossale
I need today's date - and zero anything else (" 05/06/08 00:00:00 ")
我需要今天的日期 - 并且将其他任何内容归零(“ 05/06/08 00:00:00 ”)
I've tried
我试过了
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 0);
Date date1 = calendar.getTime();
System.out.println(date1);
Run: (This is seriously messed up)
跑:(这严重搞砸了)
If the hour on the computer is < 12:00 at noon : Sun Mar 08 00:44:39 IST 2009
如果计算机上的时间在中午 < 12:00 :Sun Mar 08 00:44:39 IST 2009
If the hour on the computer is > 12:00 at noon : Sun Mar 08 12:46:53 IST 2009
如果计算机上的时间在中午 > 12:00 :Sun Mar 08 12:46:53 IST 2009
So I gave this up.
所以我放弃了这个。
All the Date's setters are deprecated (except the epoch time) - so I don't want to use them either
不推荐使用所有日期的设置器(纪元时间除外) - 所以我也不想使用它们
The only thing I could think of is
我唯一能想到的是
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String sDate = dateFormat.format(calendar.getTime());
Date today = dateFormat.parse(sDate);
But this is such a lame code I can't bring myself to write it.
但这是一个如此蹩脚的代码,我无法让自己编写它。
Any other option?
还有其他选择吗?
Thanks!
谢谢!
采纳答案by Jon Skeet
My standard advice for Java date/time questions: don't use java.util.{Calendar,Date}
. Use Joda Time. That way you can represent a date as a date(with no associated time zone), instead of a date/time. Or you could use a DateMidnight
if that's what you want to represent. (Be careful of combinations of time zone and date where there isno midnight though...)
我对 Java 日期/时间问题的标准建议:不要使用java.util.{Calendar,Date}
. 使用Joda 时间。这样您就可以将日期表示为日期(没有关联的时区),而不是日期/时间。或者你可以使用 a DateMidnight
if 那是你想要代表的。(注意时区和日期的组合,那里是没有午夜虽然...)
What do you need to use the Date
with? If you can get away with changing to use Joda throughout, that's great. Otherwise, you can use Joda to do what you want and then convert to milliseconds (and then to java.util.Date
) when you really need to.
你需要用Date
with 什么?如果您可以在整个过程中改用 Joda,那就太好了。否则,您可以使用 Joda 做您想做的事情,然后java.util.Date
在您真正需要时转换为毫秒(然后转换为)。
(Michael's solution when using Date
/Calendar
is fine if you really want to stick within a broken API... but I can't overstate how much better Joda is...)
(迈克尔在使用Date
/时的解决方案Calendar
很好,如果您真的想坚持使用损坏的 API...但我不能夸大 Joda 有多好...)
回答by Michael Borgwardt
The time component is not just hours (and Calendar.HOUR is, as you have noticed, AM/PM). You need to set allof the time fields to 0: HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND.
时间组件不仅仅是小时(而 Calendar.HOUR 是,正如您所注意到的,AM/PM)。您需要将所有时间字段设置为 0:HOUR_OF_DAY、MINUTE、SECOND、MILLISECOND。
回答by Kurley
Why the string manipulation?
为什么要进行字符串操作?
Can you not just set the values you need on the Calendar object before converting to a Date using getTime()?
在使用 getTime() 转换为 Date 之前,您不能只在 Calendar 对象上设置您需要的值吗?
回答by Damo
I use this:
我用这个:
public static Date startOfDay(Date date) {
Calendar dCal = Calendar.getInstance();
dCal.setTime(date);
dCal.set(Calendar.HOUR_OF_DAY, 0);
dCal.set(Calendar.MINUTE, 0);
dCal.set(Calendar.SECOND, 0);
dCal.set(Calendar.MILLISECOND, 0);
return dCal.getTime();
}
回答by Steve K
See Apache's commons-langDateUtils.truncate()
参见Apache 的 commons-langDateUtils.truncate()
回答by OscarRyz
You should use HOUR_OF_DAYinstead of HOURand combine it with MINUTE and SECOND also.
您应该使用HOUR_OF_DAY而不是HOUR并将其与 MINUTE 和 SECOND 结合使用。
import java.util.Calendar;
import static java.util.Calendar.HOUR_OF_DAY;
import static java.util.Calendar.MINUTE;
import static java.util.Calendar.SECOND;
import static java.util.Calendar.MILLISECOND;
public class Today {
public static void main( String [] args ) {
Calendar cal = Calendar.getInstance();
cal.set( HOUR_OF_DAY, 0 );
cal.set( MINUTE, 0 );
cal.set( SECOND, 0 );
cal.set( MILLISECOND, 0 );
System.out.println( cal.getTime() );
}
}
The results you are getting are due to HOUR is used to AM/PM while HOUR_OF_DAY is 24 hrs.
您得到的结果是由于 HOUR 用于 AM/PM 而 HOUR_OF_DAY 是 24 小时。
HOUR_OF_DAY:
HOUR_OF_DAY:
Field number for get and set indicating the hour of the day. HOUR_OF_DAY is used for the 24-hour clock. E.g., at 10:04:15.250 PM the HOUR_OF_DAY is 22.
get 和 set 的字段编号指示一天中的小时。HOUR_OF_DAY 用于 24 小时制。例如,在晚上 10:04:15.250,HOUR_OF_DAY 是 22。
HOUR:
小时:
Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.
get 和 set 的字段编号,指示上午或下午的小时数。HOUR 用于 12 小时制 (0 - 11)。中午和午夜由 0 表示,而不是 12。例如,在 10:04:15.250 PM,HOUR 是 10。
回答by OscarRyz
Another vote for JodaTime.
对 JodaTime 的另一票。
java.util.Date and Calendar are so bad they are broken. (And SimpleDateFormat is rubbish too!)
java.util.Date 和 Calendar 太糟糕了,它们都坏了。(而且 SimpleDateFormat 也是垃圾!)
For what it's worth, Java 7 will include a new date time library based strongly around JodaTime.
就其价值而言,Java 7 将包含一个基于 JodaTime 的新日期时间库。
回答by Clinton
As mentioned above you should use
如上所述,您应该使用
Calendar.HOUR_OF_DAY
As opposed to
与
Calendar.HOUR
Also you need to clear out the other fields (Calendar.MINUTE
, Calendar.SECOND
, and Calendar.MILLISECOND
) by setting them to zero.
另外你需要清除其他字段(Calendar.MINUTE
,Calendar.SECOND
以及Calendar.MILLISECOND
将它们设置为零)。
Sorry there's no easy way here. A pain, and that's why they're working on a new API for Java 7 I believe based on Joda Time.
抱歉,这里没有简单的方法。一个痛苦,这就是为什么他们正在为 Java 7 开发一个新的 API,我相信它基于 Joda Time。
回答by stenix
...or you can do it the hacker way:
...或者你可以用黑客的方式来做:
long MS_PER_DAY = 86400000L;
Date dateTime=new Date();
long offset = TimeZone.getDefault().getOffset(dateTime.getTime());
Date date= new Date(((dateTime.getTime()+offset)/MS_PER_DAY)*MS_PER_DAY-offset);
回答by zorman2000
I know this is a very old question, no longer active, but it came to be on the top when I searched Google.
我知道这是一个非常古老的问题,不再活跃,但是当我搜索谷歌时它出现在顶部。
While all advise is very good, I can't believe no one simply answered:
尽管所有建议都非常好,但我不敢相信没有人会简单地回答:
Date date = new Date(System.currentTimeMillis());
System.out.println(date);
Which returns effectively, today's date.
哪个有效地返回,今天的日期。