[英]Getting today's date in java - I've tried the regular ways
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:太阳3月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:太阳3月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 所有Date的setter都被弃用了(纪元时间除外) - 所以我也不想使用它们
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! 谢谢!
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();
}
My standard advice for Java date/time questions: don't use java.util.{Calendar,Date}
. 我对Java日期/时间问题的标准建议:不要使用java.util.{Calendar,Date}
。 Use Joda Time . 使用Joda时间 。 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. 或者您可以使用DateMidnight
如果这是您想要表示的。 (Be careful of combinations of time zone and date where there is no midnight though...) (注意时区和日期的组合在没有午夜虽然...)
What do you need to use the Date
with? 你有什么需要使用Date
? If you can get away with changing to use Joda throughout, that's great. 如果你可以随时改变使用Joda,那就太好了。 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. 否则,您可以使用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是多么好......)
You should use HOUR_OF_DAY instead of HOUR and 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. HOUR_OF_DAY用于24小时制。 Eg, at 10:04:15.250 PM the HOUR_OF_DAY is 22. 例如,在下午10:04:15.250,HOUR_OF_DAY是22。
HOUR: 小时:
Field number for get and set indicating the hour of the morning or afternoon. get和set的字段编号表示上午或下午的小时。 HOUR is used for the 12-hour clock (0 - 11). 小时用于12小时制(0 - 11)。 Noon and midnight are represented by 0, not by 12. Eg, at 10:04:15.250 PM the HOUR is 10. 中午和午夜由0表示,而不是由12表示。例如,在10:04:15.250 PM,HOUR为10。
The time component is not just hours (and Calendar.HOUR is, as you have noticed, AM/PM). 时间组件不仅仅是几小时(正如您所注意到的那样,Calendar.HOUR是AM / PM)。 You need to set all of the time fields to 0: HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND. 您需要将所有时间字段设置为0:HOUR_OF_DAY,MINUTE,SECOND,MILLISECOND。
请参阅Apache的commons-lang DateUtils.truncate()
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. 哪个有效地返回,今天的日期。
...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);
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。
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对象上设置所需的值?
Another vote for JodaTime. JodaTime的另一票。
java.util.Date and Calendar are so bad they are broken. java.util.Date和Calendar非常糟糕,它们被破坏了。 (And SimpleDateFormat is rubbish too!) (而SimpleDateFormat也是垃圾!)
For what it's worth, Java 7 will include a new date time library based strongly around JodaTime. 值得一提的是,Java 7将包含一个基于JodaTime的新日期时间库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.