Java 将日期与今天日期核对
我已经编写了一些代码来检查两个日期,一个开始日期和一个结束日期。如果结束日期早于开始日期,它将给出提示,说明结束日期早于开始日期 我还想添加一个检查,检查开始日期是否早于今天(即用户使用应用程序的当天),我将如何执行此操作?(下面是日期检查代码,如果有任何关系,所有这些都是为android编写的) 这有用吗Java 将日期与今天日期核对,java,android,date,Java,Android,Date,我已经编写了一些代码来检查两个日期,一个开始日期和一个结束日期。如果结束日期早于开始日期,它将给出提示,说明结束日期早于开始日期 我还想添加一个检查,检查开始日期是否早于今天(即用户使用应用程序的当天),我将如何执行此操作?(下面是日期检查代码,如果有任何关系,所有这些都是为android编写的) 这有用吗 Calendar c = Calendar.getInstance(); // set the calendar to start of today c.set(Calendar.HOUR
Calendar c = Calendar.getInstance();
// set the calendar to start of today
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// and get that as a Date
Date today = c.getTime();
// or as a timestamp in milliseconds
long todayInMillis = c.getTimeInMillis();
// user-specified date which you are testing
// let's say the components come from a form or something
int year = 2011;
int month = 5;
int dayOfMonth = 20;
// reuse the calendar to set user specified date
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// and get that as a Date
Date dateSpecified = c.getTime();
// test your condition
if (dateSpecified.before(today)) {
System.err.println("Date specified [" + dateSpecified + "] is before today [" + today + "]");
} else {
System.err.println("Date specified [" + dateSpecified + "] is NOT before today [" + today + "]");
}
我假设您使用整数来表示年、月和日?如果要保持一致,请使用Date方法
Calendar cal = new Calendar();
int currentYear, currentMonth, currentDay;
currentYear = cal.get(Calendar.YEAR);
currentMonth = cal.get(Calendar.MONTH);
currentDay = cal.get(Calendar.DAY_OF_WEEK);
if(startYear < currentYear)
{
message = message + "Start Date is Before Today" + "\n";
}
else if(startMonth < currentMonth && startYear <= currentYear)
{
message = message + "Start Date is Before Today" + "\n";
}
else if(startDay < currentDay && startMonth <= currentMonth && startYear <= currentYear)
{
message = message + "Start Date is Before Today" + "\n";
}
Calendar cal=新日历();
int currentYear、currentMonth、currentDay;
currentYear=cal.get(日历年);
currentMonth=cal.get(日历月);
currentDay=cal.get(日历日/周);
如果(起始日期<当前年份)
{
message=message+“开始日期早于今天”+“\n”;
}
否则,如果(startMonth
DateMidnight startDate = new DateMidnight(startYear, startMonth, startDay);
if (startDate.isBeforeNow())
{
// startDate is before now
// do something...
}
要检查某个日期是否为今天的日期,或者仅检查该日期中是否包含时间,请将时间设为00:00:00,并使用下面的代码
Calendar c = Calendar.getInstance();
// set the calendar to start of today
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Date today = c.getTime();
// or as a timestamp in milliseconds
long todayInMillis = c.getTimeInMillis();
int dayOfMonth = 24;
int month = 4;
int year =2013;
// reuse the calendar to set user specified date
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month - 1);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// and get that as a Date
Date dateSpecified = c.getTime();
// test your condition
if (dateSpecified.before(today)) {
Log.v(" date is previou")
} else if (dateSpecified.equal(today)) {
Log.v(" date is today ")
}
else if (dateSpecified.after(today)) {
Log.v(" date is future date ")
}
希望它能帮助….不要让它复杂化。使用这个简单的方法。导入DateUtils java类并调用以下返回布尔值的方法
boolean isBeforeToday(Date d) {
Date today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(0);
return d.before(today);
}
DateUtils.isSameDay(date1,date2);
DateUtils.isSameDay(calender1,calender2);
DateUtils.isToday(date1);
有关更多信息,请参阅本文,了解执行此操作的另一种方法:
public class TimeUtils {
/**
* @param timestamp
* @return
*/
public static boolean isToday(long timestamp) {
Calendar now = Calendar.getInstance();
Calendar timeToCheck = Calendar.getInstance();
timeToCheck.setTimeInMillis(timestamp);
return (now.get(Calendar.YEAR) == timeToCheck.get(Calendar.YEAR)
&& now.get(Calendar.DAY_OF_YEAR) == timeToCheck.get(Calendar.DAY_OF_YEAR));
}
}
试试这个:
public static boolean isToday(Date date)
{
return org.apache.commons.lang3.time.DateUtils.isSameDay(Calendar.getInstance().getTime(),date);
}
太长,读不下去了
LocalDate
.parse(“2021-01-23”)
.isBefore(
LocalDate.now(
“非洲/突尼斯”地区
)
)
…或:
试试看
{
org.threeten.extra.LocalDateRange范围=
LocalDateRange.of(
本地日期(“2021-01-23”),
本地日期(“2021-02-21”)
)
;
如果(范围.isAfter(
LocalDate.now(非洲/突尼斯地区)
) { … }
否则{…今天的句柄在范围之内或之后。}
}catch(java.time.DateTimeException e){
//处理结束在开始之前的错误。
}
细节
其他答案忽略了关键的问题
其他答案使用过时的类
避免旧的日期时间类
与最早版本的Java捆绑在一起的旧日期时间类设计拙劣、混乱且麻烦。请避免使用Java.util.date/.Calendar和相关类
java.time
- 在Java8及更高版本中,使用内置框架。请参阅
- 在Java7或Java6中,将添加到项目中
- 在中,使用
LocalDate
对于仅日期的值,不带时间和时区,请使用类
时区
请注意,虽然LocalDate
不存储时区,但确定诸如“今天”之类的日期需要一个时区。对于任何给定时刻,世界各地的日期可能因时区而异。例如,巴黎的新一天比蒙特勒尔的黎明早。巴黎午夜后的一刻在蒙特勒尔仍然是“昨天”
如果您只有一个,请使用。如果您有一个完整的时区(大陆/地区),请使用。如果您想要UTC,请使用handy常量
使用isEqual
、isBefore
和isAfter
方法比较很容易
boolean invalidInterval = stop.isBefore( start );
我们可以检查今天是否包含在这个日期范围内。在我这里展示的逻辑中,我使用半开放的方法,其中开始是包含的,而结束是独占的。这种方法在日期时间工作中很常见。例如,一周从一个星期一一直到下一个星期一,但不包括下一个星期一
// Is today equal or after start (not before) AND today is before stop.
boolean intervalContainsToday = ( ! today.isBefore( start ) ) && today.isBefore( stop ) ) ;
LocalDateRange
如果使用这些时间跨度进行广泛的工作,考虑将库添加到项目中。这个库扩展了Java.TimeFrand,并且是可能添加到java的验证地。Time.T/P>
ThreeTen Extra包含一个类,该类具有方便的方法,例如
邻接
,包含
,包围
,重叠
,等等
关于java.time
该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,如,&
该项目现已启动,建议迁移到类
要了解更多信息,请参阅.和搜索堆栈溢出以获取许多示例和解释。规范为
您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要java.sql.*
classes
从哪里获得java.time类
- 然后
- 内置的
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
- 更高版本的Android捆绑包实现了java.time类
- 对于早期的Android(Android已经有一个专门的类用于此。请使用纯Java检查:
public static boolean isToday(Date date){
Calendar today = Calendar.getInstance();
Calendar specifiedDate = Calendar.getInstance();
specifiedDate.setTime(date);
return today.get(Calendar.DAY_OF_MONTH) == specifiedDate.get(Calendar.DAY_OF_MONTH)
&& today.get(Calendar.MONTH) == specifiedDate.get(Calendar.MONTH)
&& today.get(Calendar.YEAR) == specifiedDate.get(Calendar.YEAR);
}
毫秒不应该在开始时重置为零吗?使用Joda Time的DateMidnight,您不需要重置任何内容。毫秒还需要重置c.set(Calendar.millis秒,0);如果您不这样做,那么如果您比较当天的日期,就会出现问题。使用日历.HOUR
是错误的,因为它不会重置AM/PM属性,因此如果当前时间为13 PM,则生成的小时将为12 PM。改为使用日历.HOUR\u OF_day
。更新:这些糟糕的日期时间类现在已被mo取代JSR 310中定义的dern java.time类。现在已经弃用了!在Month参数中也要非常小心-Joda的范围在1-12之间,java的范围在0-11之间。如果月份是一月并从java传递,则可以引发IllegalArgument异常。虽然没有文档记录,但它会引发org.Joda.time.IllegalFieldValueException(IllegalArgumentException的子类)如果该值无效。很好的提示,但是IMO Joda的范围
boolean invalidInterval = stop.isBefore( start );
// Is today equal or after start (not before) AND today is before stop.
boolean intervalContainsToday = ( ! today.isBefore( start ) ) && today.isBefore( stop ) ) ;
public static boolean isToday(Date date){
Calendar today = Calendar.getInstance();
Calendar specifiedDate = Calendar.getInstance();
specifiedDate.setTime(date);
return today.get(Calendar.DAY_OF_MONTH) == specifiedDate.get(Calendar.DAY_OF_MONTH)
&& today.get(Calendar.MONTH) == specifiedDate.get(Calendar.MONTH)
&& today.get(Calendar.YEAR) == specifiedDate.get(Calendar.YEAR);
}
public static boolean itIsToday(long date){
boolean result = false;
try{
Calendar calendarData = Calendar.getInstance();
calendarData.setTimeInMillis(date);
calendarData.set(Calendar.HOUR_OF_DAY, 0);
calendarData.set(Calendar.MINUTE, 0);
calendarData.set(Calendar.SECOND, 0);
calendarData.set(Calendar.MILLISECOND, 0);
Calendar calendarToday = Calendar.getInstance();
calendarToday.setTimeInMillis(System.currentTimeMillis());
calendarToday.set(Calendar.HOUR_OF_DAY, 0);
calendarToday.set(Calendar.MINUTE, 0);
calendarToday.set(Calendar.SECOND, 0);
calendarToday.set(Calendar.MILLISECOND, 0);
if(calendarToday.getTimeInMillis() == calendarData.getTimeInMillis()) {
result = true;
}
}catch (Exception exception){
Log.e(TAG, exception);
}
return result;
}