Tin*_*iny 6 java datetime jodatime date-comparison
我们假设我有两个日期,如下所示.
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45");
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45");
Run Code Online (Sandbox Code Playgroud)
我想比较这两个日期,看看firstDate是早,晚或等secondDate.
我可以尝试以下方法.
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isBefore(secondDate));
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.isAfter(secondDate));
System.out.println("firstDate = "+firstDate+"\nsecondDate = "+secondDate+"\ncomparison = "+firstDate.equals(secondDate));
Run Code Online (Sandbox Code Playgroud)
这段代码产生的正是我想要的.
它产生以下输出.
firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = true
firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = false
firstDate = 2012-02-16T12:03:45.000+05:30
secondDate = 2013-02-17T12:03:45.000+05:30
comparison = false
Run Code Online (Sandbox Code Playgroud)
我需要忽略这些日期的秒和毫秒部分.我试图使用以下方法withSecondOfMinute(0)和withMillis(0)方法.
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillis(0);
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillis(0);
Run Code Online (Sandbox Code Playgroud)
但它产生了以下输出.
firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = false
firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = false
firstDate = 1970-01-01T05:30:00.000+05:30
secondDate = 1970-01-01T05:30:00.000+05:30
comparison = true
Run Code Online (Sandbox Code Playgroud)
该withSecondOfMinute()方法的文档描述.
返回此日期时间的副本,并更新第二个分钟字段.DateTime是不可变的,因此没有set方法.相反,此方法返回一个新实例,其值为second of minute更改.
该方法的文档withMillis()说.
以不同的millis返回此datetime的副本.返回的对象将是新实例或此实例.只有millis会改变,时间和时区保持不变.
通过完全忽略时间部分来比较日期可以使用DateTimeComparator.getDateOnlyInstance()大致如下的方式轻松完成.
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)==0){}
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)<0){}
if(DateTimeComparator.getDateOnlyInstance().compare(firstDate, secondDate)>0){}
Run Code Online (Sandbox Code Playgroud)
如何比较忽略特定瞬间的两个日期DateTime(在这种情况下是秒和毫秒)?
另一种方法是创建一个具有分钟下限的DateTimeComparator:
DateTimeComparator comparator = DateTimeComparator.getInstance(
DateTimeFieldType.minuteOfHour());
Run Code Online (Sandbox Code Playgroud)
这将忽略被比较对象的第二和毫秒部分.
我想你想用DateTime#withMillisOfSecond()而不是DateTime#withMillis():
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm:ss").withZone(DateTimeZone.forID("Asia/Kolkata"));
DateTime firstDate = formatter.parseDateTime("16-Feb-2012 12:03:45").withSecondOfMinute(0).withMillisOfSecond(0);
DateTime secondDate = formatter.parseDateTime("17-Feb-2013 12:03:45").withSecondOfMinute(0).withMillisOfSecond(0);
Run Code Online (Sandbox Code Playgroud)
设置DateTime#withMillis()到0,都将日期复位1/1/1970,因此你true对equals他们的呼叫.
同样,设置DateTime#withMillisOfDay()为0,将设置时间midnight.