Unable to find difference between two dates w.r.t time,seconds,months and years?

StackOverflow https://stackoverflow.com/questions/22930254

  •  29-06-2023
  •  | 
  •  

Pergunta

This is my java class

   public class dateparse {
   public static void main(String args[]) throws ParseException
   {
   Date dd=new Date();
   int year = Calendar.getInstance().get(Calendar.YEAR);
    int month=0;
   int calc_days=0;
   String d1 = dd.getDate()+"/"+dd.getMonth()+"/"+year;
   String d2 = "19/1/2014";
   SimpleDateFormat s1 = new SimpleDateFormat("dd/mm/yyyy");
   SimpleDateFormat s2 = new SimpleDateFormat("dd/mm/yyyy");
   Date dateOne = new SimpleDateFormat("dd/mm/yyyy").parse(d1);
   Date dateTwo = s2.parse(d2);
   long diff = dateOne.getTime() - dateTwo.getTime();
   calc_days=    (int) (diff / 1000 / 60 / 60 / 24 / 1);
    }
   }

I am trying to find the difference between current date and the date specified with respect to seconds,minutes,hours,days,months and years.Here my input date is 19th Feb 2014.I want to show the difference in no of days(e.g. 10 days) or months+days(e.g.1 month and 2 days) or year+month+days(e.g. 1 year and 2 months and 4 days).But when I run this code it returns difference as -10 days.

Foi útil?

Solução

Your error is your parsing. Lowercase m means minutes, not month:

SimpleDateFormat s2 = new SimpleDateFormat("dd/mm/yyyy");

should be:

SimpleDateFormat s2 = new SimpleDateFormat("dd/MM/yyyy");

Here's a simplified example:

String d1 = "21/1/2014";
String d2 = "19/1/2014";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date dateOne = sdf.parse(d1);
Date dateTwo = sdf.parse(d2);
long diff = dateOne.getTime() - dateTwo.getTime();
int differenceInDays = (int) (diff / 1000 / 60 / 60 / 24 / 1);
System.out.println(differenceInDays);

Prints: 2

Outras dicas

This is a classic error caused by the horrible API that Java has provided: date.getMonth() returns 0 for January, 1 for february... and 11 for December. If you can, try to avoid java.util.Date and Calendar :P

Attention - Accepted answer is wrong! Prove:

Use as input the dates 2014-03-19 and 2014-04-01 in my timezone "Europe/Berlin". The true answer is 13 days as everyone can easily veryify using standard calendars, but the accepted code of @Duncan produces 12 days because in my timezone there was a dst-jump which breaks the basis of calculation formular (1 day = 24 hours). On 30th of March the day was only 23 hours long.

The JDK pre 8 does not offer a built-in generic solution for this problem. Please also note that your input is just a pair of two plain dates with no time. Therefore it is silly to ask for the difference in seconds, etc. Only asking for the difference in days, months, weeks or years is sensible. In Java 8 you can do following:

// only days
LocalDate start = LocalDate.of(2014, 3, 19); // start in March
LocalDate end = LocalDate.of(2014, 4, 1);
int days = ChronoUnit.DAYS.between(start, end); // 13

// period in years, months and days
LocalDate start = LocalDate.of(2014, 2, 19); // start in February
LocalDate end = LocalDate.of(2014, 4, 1);
Period period = Period.between(start, end); // P1M13D = 1 month + 13 days

Unfortunately you are not free to choose in which calendar units you like to get the difference expressed. JodaTime (and my library) has a more flexible approach using PeriodType.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top