2012-02-10 100 views
-1

我将有2个日期,即开始日期和结束日期。我要计算的日期两者结合,然后是持续时间应该有以下三个条件必须检查所有的时间 -Java日期持续时间验证

  1. 如果持续时间等于(1年+ - 5天),然后打印“Hello”。
  2. 如果持续时间小于(1年-5天),则打印“Hi”。
  3. 如果持续时间大于(1年+ 5天),则打印“Hi”。

请帮助我,让我知道是否有任何其他信息是必需的。

我更喜欢使用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"); 
     } 
    } 
} 

在此先感谢。

+0

最后两个条件似乎滑稽。 – 2012-02-10 12:39:57

+0

那有什么好笑的。如果持续时间低于和高于所述时间段,则应执行一些逻辑,否则应该执行一些其他逻辑。 – rahul 2012-02-10 13:05:28

+0

抱歉,没有伤害的意思。 – 2012-02-10 13:45:30

回答

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块。

+0

如果您只是检查两个日期之间有少于360天或超过370天,您没有考虑闰年。 “1年+ 5天”这段时间的长短取决于它是否是闰年。 – Jesper 2012-02-10 14:43:16

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"); 
} 
+0

谢谢Jesper。你的解决方案真的有用 – rahul 2012-02-13 05:18:53

+0

用我的最终程序更新问题部分。 – rahul 2012-02-13 05:20:51

0

我者优先使用约达时间的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"); 
    } 

} 

}