Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

java - 如何在Java中比较日期? [重复](How to compare dates in Java? [duplicate])

How do I compare dates in between in Java? (如何在Java之间比较日期?)

Example: (例:)

date1 is 22-02-2010 (date1是22-02-2010)
date2 is 07-04-2010 today (date2是今天的07-04-2010)
date3 is 25-12-2010 (date3是25-12-2010)

date3 is always greater than date1 and date2 is always today. (date3始终大于date1date2始终是今天。) How do I verify if today's date is in between date1 and date 3? (如何验证今天的日期是否在date1和date 3之间?)

  ask by ant translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Date has before and after methods and can be compared to each other as follows: (日期之前之后的方法,可以相互比较 ,如下所示:)

if(todayDate.after(historyDate) && todayDate.before(futureDate)) {
    // In between
}

For an inclusive comparison: (进行包容性比较:)

if(!historyDate.after(todayDate) && !futureDate.before(todayDate)) {
    /* historyDate <= todayDate <= futureDate */ 
}

You could also give Joda-Time a go, but note that: (您也可以尝试一下Joda-Time ,但请注意:)

Joda-Time is the de facto standard date and time library for Java prior to Java SE 8. Users are now asked to migrate to java.time ( JSR-310 ). (Joda-Time是Java SE 8之前的Java的事实上的标准日期和时间库。现在,要求用户迁移到java.timeJSR-310 )。)

Back-ports are available for Java 6 and 7 as well as Android. (反向端口适用于Java 6和7以及Android。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...