In this tutorial, you will see how to calculate the number of days in between two dates.
Calculate number of days in between two date.
This is a java code example. It is used for calculating the number of days in between two days. Here we are using a Calendar class of java. It present in java.util package. The getInstance() method of Calendar class gets calendar with default time Zone, and the set(int yyyy, int mm, int dd) method sets the value of set() method's fields. After this we are converting date in millisecond. Here, we are repeating this process for second date.
Code:import java.util.Calendar; public class DayBetweenTwoDate { public static void main(String[] args) { Calendar date1 = Calendar.getInstance(); date1.set(2010, 10, 23); long dateMillisecond = date1.getTimeInMillis(); System.out.println("Value of date1 in MilliSecond : " + dateMilliSecond); Calendar date2 = Calendar.getInstance(); date2.set(2010, 11, 23); long dateMilliSecond2 = date2.getTimeInMillis(); System.out.println("Value of date2 in MilliSecond : " + dateMilliSecond2); long differMilliSecond = dateMiliSecond2 - dateMilliSecond; System.out.println("Difference of Milisecond : " + differMiliSecond); long diffSecond = differMilliSecond / 1000; System.out.println("Difference of second : " + diffSecond); long diffMinutes = diffSecond / 60; System.out.println("Difference of minutes : " + diffMinutes); long diffHours = diffMinutes / 60; System.out.println("Difference of hours : " + diffHours); long differDays = diffHours / 24; System.out.println("Difference of days : " + differDays); } }Output
Value of date1 in Millisecond : 1290496927343
Value of date2 in Millisecond : 1293088927359 Difference of Millisecond : 2592000016 Difference of second : 2592000 Difference of minutes : 43200 Difference of hours : 720 Difference of days : 30 |
[ 0 ] Comments