Zaa*_*ask 31 java android date
我写了一些代码来检查两个日期,开始日期和结束日期.如果结束日期在开始日期之前,则会提示结束日期在开始日期之前.
我还想添加一个检查,如果开始日期是在今天之前(今天是在用户使用该应用程序的那天)我该怎么做?(下面的日期检查程序代码,如果有任何影响,所有这些都是为Android编写的)
if (startYear > endYear) {
fill = fill + 1;
message = message + "End Date is Before Start Date" + "\n";
} else if (startMonth > endMonth && startYear >= endYear) {
fill = fill + 1;
message = message + "End Date is Before Start Date" + "\n";
} else if (startDay > endDay && startMonth >= endMonth && startYear >= endYear) {
fill = fill + 1;
message = message + "End Date is Before Start Date" + "\n";
}
Run Code Online (Sandbox Code Playgroud)
Kis*_*ath 118
不要那么复杂.使用这种简单的方法.导入DateUtils java类并调用以下返回布尔值的方法.
DateUtils.isSameDay(date1,date2);
DateUtils.isSameDay(calender1,calender2);
DateUtils.isToday(date1);
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅本文DateUtils Java
sud*_*ode 44
这有帮助吗?
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 + "]");
}
Run Code Online (Sandbox Code Playgroud)
其他答案忽略了时区的关键问题.
其他答案使用过时的类.
与最早版本的Java捆绑在一起的旧日期时间类设计糟糕,令人困惑且麻烦.避免使用java.util.Date/.Calendar和相关的类.
LocalDate对于仅限日期的值,没有时间和没有时区,请使用LocalDate该类.
LocalDate start = LocalDate.of( 2016 , 1 , 1 );
LocalDate stop = start.plusWeeks( 1 );
Run Code Online (Sandbox Code Playgroud)
请注意,虽然LocalDate不存储时区,但确定诸如"今天"之类的日期需要时区.对于任何特定时刻,日期可能因时区而异.例如,巴黎的新日早些时候比蒙特利尔早.巴黎午夜过后的一刻仍然在蒙特利尔的"昨天".
如果您拥有的是UTC的偏移量,请使用ZoneOffset.如果您有全时区(大陆/地区),请使用ZoneId.如果您想要UTC,请使用方便的常量ZoneOffset.UTC.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
Run Code Online (Sandbox Code Playgroud)
比较容易用isEqual,isBefore和isAfter方法.
boolean invalidInterval = stop.isBefore( start );
Run Code Online (Sandbox Code Playgroud)
我们可以查看今天是否包含在此日期范围内.在我这里显示的逻辑中,我使用半开放方法,其中开头是包容性的,而结尾是独占的.这种方法在日期时间工作中很常见.因此,例如,一周从周一开始,但不包括下周一.
// Is today equal or after start (not before) AND today is before stop.
boolean intervalContainsToday = ( ! today.isBefore( start ) ) && today.isBefore( stop ) ) ;
Run Code Online (Sandbox Code Playgroud)
Interval如果在这样的时间范围内进行广泛的工作,请考虑将ThreeTen-Extra库添加到项目中.这个库扩展了java.time框架,并且是可能添加到java.time的试验场.
ThreeTen-额外包括Interval与方便的方法,如类abuts,contains,encloses,overlaps,等.
该java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.
现在处于维护模式的Joda-Time项目建议迁移到java.time类.
要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.
您可以直接与数据库交换java.time对象.使用符合JDBC 4.2或更高版本的JDBC驱动程序.不需要字符串,不需要课程.java.sql.*
从哪里获取java.time类?
使用Joda Time可以简化为:
DateMidnight startDate = new DateMidnight(startYear, startMonth, startDay);
if (startDate.isBeforeNow())
{
// startDate is before now
// do something...
}
Run Code Online (Sandbox Code Playgroud)
检查日期是否是今天的日期,或者不仅检查日期而不是包含在其中的时间,因此将时间设为 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 ")
}
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助......
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
100529 次 |
| 最近记录: |