0

This is my code,

e.g. 2year, 2 month, 1 week 2 days, 1 hour, 2 minutes , 35 seconds.

    String stdate ="01/01/2014 09:30:30";
    String endate ="09/11/2015 11:30:30";
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    Date d1 = new Date();
    Date d2 = new Date();
    long year =(1000*60*60*24*365l);
    long month =(1000*60*60*24*30l);
    long weeks =(1000*60*60*24*7l);
    long days =(1000*60*60*24l);

    try{

        d1 = df.parse(stdate);
        d2 = df.parse(endate);
        long diff = d2.getTime()-d1.getTime();
        long diffYear = diff/(1000*60*60*24*365l);
        long diffMonth = (diff-(diffYear*year))/month;
        long diffWeeks = ((diff%month))/weeks;
        long diffDays = ((diff%weeks))/days;

        System.out.println(diffYear+" years  ");
        System.out.println(diffMonth+" months ");
        System.out.println(diffWeeks+" week ");
        System.out.println(diffDays+" days "); // wrong output,
    }catch(Exception e){

    }

o/p:

1 years
10 months
2 week
5 days

I do not want to use joda time. it should be in java.util.*;

Please answer, thanks in advance.

Rishi Dwivedi
  • 908
  • 5
  • 19

1 Answers1

3

You can try using Period from Joda-time

String stdate = "01/01/2014 09:30:30";
String endate = "09/11/2015 11:30:30";
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Date d1 = df.parse(stdate);
Date d2 = df.parse(endate);;

DateTime startTime = new DateTime(d1), endTime = new DateTime(d2);
Period p = new Period(startTime, endTime);
System.out.printf("%-8s %d %n","years:",p.getYears());
System.out.printf("%-8s %d %n","months:",p.getMonths());
System.out.printf("%-8s %d %n","weeks:",p.getWeeks());
System.out.printf("%-8s %d %n","days:",p.getDays());
System.out.printf("%-8s %d %n","hours:",p.getHours());
System.out.printf("%-8s %d %n","minutes:",p.getMinutes());
System.out.printf("%-8s %d %n","second:",p.getSeconds());

Output:

years:   1 
months:  10 
weeks:   1 
days:    1 
hours:   2 
minutes: 0 
second:  0 

Update:

To answer your original question: Just like you are subtracting days that already belongs to year from diff to calculate months, you need to subtract

  • sum of days used on year and month to calculate weeks
  • sum of days which already belong to year, month, week if you want to calculate days

so your code can look like

long diffYear = diff / year;
long diffMonth = (diff - (diffYear * year)) / month;
//long diffWeeks= ((diff % month)) / weeks;
long diffWeeks = (diff - (diffYear * year + diffMonth * month)) / weeks;
//long diffDays = ((diff % weeks)) / days;
long diffDays = (diff - (diffYear * year + diffMonth * month + diffWeeks*weeks)) / days;//((diff % weeks)) / days;

WARNING: This way of calculating days or weeks will treat each month as 30 days, which is not always true, because there can be also 28,29,30,31 day months. That is why instead of 1 day you will see 5.

WARNING 2: Instead of 1000*60*60*24*365l which for larger numbers can cause integer overflow (first numbers used are integers) you should use 1000L*60*60*24*365. So

  • change l to L because l looks like 1 so it can be confusing,
  • start multiplying with long.

To make things easier you can even write it as

long seconds = 1000L;
long minutes = seconds * 60;
long hours = minutes * 60;
long days = hours * 24;
long weeks = days * 7;
long month = days * 30;
long year = days * 365;
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • is it possible in java too?? – Ashish Ratan Feb 23 '14 at 12:22
  • @AshishRatan Joda is written in Java, so yes. – Pshemo Feb 23 '14 at 12:37
  • thanks 1+, after having +15 reputation, – Rishi Dwivedi Feb 23 '14 at 12:47
  • +1 but odd that his question says "I do not want to use joda time" yet he liked this answer. Good thing you posted a joda solution (which is the best way) – Bohemian Feb 23 '14 at 13:53
  • @Bohemian Yes, question was updated after I posted this answer about Joda :) – Pshemo Feb 23 '14 at 16:30
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). The java.time classes include `Period` & `Duration` classes. The *ThreeTen-Extra* project offers `Interval`. – Basil Bourque Jan 02 '18 at 00:24