Java日期持续时间验证

时间:2012-02-10 12:35:40

标签: java java-ee

我将有2个日期,即开始日期和结束日期。我必须计算两个日期之间的持续时间,然后应检查所有低于3个条件的持续时间 -

  1. 如果持续时间等于(1年+ - 5天),则打印“Hello”。
  2. 如果持续时间小于(1年-5天),则打印“嗨”。
  3. 如果持续时间大于(1年+5天),则打印“嗨”。
  4. 如果需要更多信息,请帮助我并告诉我。

    我更喜欢使用joda time api。持续时间计算非常简单。我有问题将条件用以上所述3条件进行验证。无论如何我在我的程序中放入的条件似乎不合适。

    import org.joda.time.DateTime;
    import org.joda.time.Period;
    
    class Test {
    
        public static void main(String[] args) {
            DateTime d1 = new DateTime(2011, 1, 1, 0, 0, 0, 0);
            DateTime d2 = new DateTime(2012, 1, 1, 0, 0, 0, 0);
            Period p1 = new Period(d1, d2);
            if (p1.getYears()==1 && p1.getMonths()==0 &&
                    (p1.getDays()<5||p1.getDays()>5)) {
                System.out.println("Hello");
            } else {
                System.out.println("Hi");
            }
        }
    }
    

    先谢谢。

3 个答案:

答案 0 :(得分:2)

使用以下类,该方法具有daysBetween方法,可用于计算天数。

 public class DateDifference {
 public static void main(String args[]){
 DateDifference difference = new DateDifference();
 }
 DateDifference() {
 Calendar cal1 = new GregorianCalendar();
 Calendar cal2 = new GregorianCalendar();

 cal1.set(2008, 8, 1); 
 cal2.set(2008, 9, 31);
 System.out.println("Days= "+daysBetween(cal1.getTime(),cal2.getTime()));
 }
 public int daysBetween(Date d1, Date d2){
 return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
 }
 } 

我认为你不再需要帮助,除非你不知道一年中有多少天而且你不知道如何在java中使用if...else块。

答案 1 :(得分:0)

你必须注意1年的持续时间并不总是固定的,因为有些年份有365天,其他日子有366天。因此,您不能简单地计算日期之间的天数,而不考虑闰年;这并不总能带来正确的结果。

我会这样做:

DateTime start = d1.plusYears(1).minusDays(5);
DateTime end = d1.plusYears(1).plusDays(5);

if (d2.isBefore(start)) {
    System.out.println("Difference between d1 and d2 is less than 1 year - 5 days");
} else if (d2.isAfter(end)) {
    System.out.println("Difference between d1 and d2 is more than 1 year + 5 days");
} else {
    System.out.println("Difference is more than or equal to 1 year + 5 days " +
                       "and less than or equal to 1 year - 5 days");
}

答案 2 :(得分:0)

我更喜欢使用joda time api。持续时间计算非常简单。我有问题将条件用以上所述3条件进行验证。无论如何我在我的程序中放入的条件似乎不合适。

import org.joda.time.DateTime;
import org.joda.time.Period;

class Test {

public static void main(String[] args) {
    DateTime d1 = new DateTime(2011, 1, 1, 0, 0, 0, 0);
    DateTime d2 = new DateTime(2012, 1, 1, 0, 0, 0, 0);
    Period p1 = new Period(d1, d2);
     if(p1.getYears()==1 && p1.getMonths()==0 &&
     (p1.getDays()<5||p1.getDays()>5)){
    System.out.println("Hello");
     }else{
    System.out.println("Hi");
               }

}

}

这是我的计划,它符合我的期望 -

import org.joda.time.DateTime;

class Test {

public static void main(String[] args) {
    DateTime d1 = new DateTime(2011, 1, 1, 0, 0, 0, 0);
    DateTime d2 = new DateTime(2012, 1, 1, 0, 0, 0, 0);
    DateTime start=d1.plusYears(1).minusDays(5);
    DateTime end=d1.plusYears(1).plusDays(5);
    if(d2.isBefore(start)||d2.isAfter(end)){
        System.out.println("HI");
    }else{
        System.out.println("HELLO");
    }

}

}