使用 java 使用 Calendar 类比较日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10624229/
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
Compare dates using Calendar class using java
提问by Do Good
The user entered date is using a drop down separately for day, month and year. I have to compare the user entered date with today's date and check if it is same day or future day. I am a bit confused about the time portion because I am not interested in time, just the date. How to solve this without using the Date class (I read it is not recommended to use Date class).
用户输入的日期使用下拉菜单分别表示日、月和年。我必须将用户输入的日期与今天的日期进行比较,并检查它是同一天还是未来的一天。我对时间部分有点困惑,因为我对时间不感兴趣,只对日期感兴趣。如何在不使用 Date 类的情况下解决这个问题(我读过不建议使用 Date 类)。
Thanks.
谢谢。
回答by Tomasz Nurkiewicz
You first need to create GregorianCalendar
instance representing entered date:
您首先需要创建GregorianCalendar
代表输入日期的实例:
Calendar user = new GregorianCalendar(2012, Calendar.MAY, 17);
And one for "now":
一个是“现在”:
Calendar now = new GregorianCalendar();
This will yield positive value if date is in the future and negative - if in the past:
如果日期在未来,则这将产生正值,如果在过去,则会产生负值:
user.compareTo(now);
Few notes about constructing user
object:
关于构造user
对象的一些注意事项:
- it uses current JVM time zone, so in my case it is midnight, 17th of May in CEST time zone
- be careful with months, they are 0-based
- 它使用当前的 JVM 时区,所以在我的情况下是 CEST 时区的 5 月 17 日午夜
- 小心月份,它们是基于 0 的
回答by A_G
Try class DateUtils of library Apache Commons Lang.
试试类库 Apache Commons Lang 的 DateUtils。
This class provides the method truncatedEquals(cal1, cal2, field).
此类提供了 truncatedEquals(cal1, cal2, field) 方法。
So you can check for equality with a single line of code:
因此,您可以使用一行代码检查相等性:
Calendar user = new GregorianCalendar(2012, Calendar.MAY, 17);
Calendar now = Calendar.getInstance();
if(DateUtils.truncatedEquals(user, now, Calendar.DATE)){
// your code goes here
}
回答by Mrseguin
Simple calculation :
简单计算:
GregorianCalendar gc1 = new GregorianCalendar();
GregorianCalendar gc2 = new GregorianCalendar();
gc2.add(GregorianCalendar.DAY_OF_MONTH, 2); // gc2 is 2 days after gc1
long duration = (gc2.getTimeInMillis() - gc1.getTimeInMillis() )
/ ( 1000 * 60 * 60 * 24) ;
System.out.println(duration);
-> 2
-> 2
回答by Xeon
Try this solution:
试试这个解决方案:
int day = 0; //user provided
int month = 0; //user provided
int year = 0; //user provided
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONDAY, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
long millisUser = calendar.getTime().getTime();
long nowMillis = System.currentTimeMillis();
if(nowMillis < millisUser) {
...
}
Above is check if date is in future.
以上是检查日期是否在未来。
There is nothing wrong in using java.util.Date
You can use:
使用没有任何问题java.util.Date
您可以使用:
int day = 0; //user provided
int month = 0; //user provided
int year = 0; //user provided
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONDAY, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
Date userSubmit = calendar.getTime();
Date now = new Date();
if(userSubmit.after(now)) {
...
}
But if you want fluent, easy and intuitive API with dates I recommend using JodaTime
但是如果你想要流畅、简单和直观的带有日期的 API,我推荐使用JodaTime
回答by ChadNC
Use a gregorian calendar. If you wanted to know the number of days difference between two dates then you could make a method similar to the following.
使用公历。如果您想知道两个日期之间相差的天数,那么您可以使用类似于以下的方法。
public int getDays(GregorianCalendar g1, GregorianCalendar g2) {
int elapsed = 0;
GregorianCalendar gc1, gc2;
if (g2.after(g1)) {
gc2 = (GregorianCalendar) g2.clone();
gc1 = (GregorianCalendar) g1.clone();
}
else {
gc2 = (GregorianCalendar) g1.clone();
gc1 = (GregorianCalendar) g2.clone();
}
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY);
while ( gc1.before(gc2) ) {
gc1.add(Calendar.DATE, 1);
elapsed++;
}
return elapsed;
}
}
That would return you the difference in the number of days.
这将返回天数的差异。