Tuesday, January 13, 2015

Java: calculate the number of days between two Dates

The following procedures help you calculate days between two Dates.

1. Java 8:
      LocalDate d1 = LocalDate.of (2014, 05, 11);
      LocalDate d2 = LocalDate.of (1998, 12, 23);

      long dayDiff = d2.until(d1, DAYS);

2. Before Java 8
      String sDate1 = "05/11/2014";
      String sDate2 = "12/23/1998";

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

      Date d1 = sdf.parse(sDate1);
      Date d2 = sdf.parse(sDate2);

      long timeDiff = d1.getTimeInMillis() - d2.getTimeInMillis();

      long dayDiff = TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS);
      //or convert the time to days using code below
      //long dayDiff = timeDiff / 1000 /(24 * 60 * 60);

------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment