Results 1 to 6 of 6

Thread: Program to calculate days difference between two dates in java.

  1. #1
    Join Date
    Aug 2009
    Posts
    57

    Program to calculate days difference between two dates in java.

    Hello friends,
    I am last year B.Sc.I.T. student. I recently started learning java language. In our last lecture our sir has given one program like "Write a Program to calculate days difference between two dates in java", but none of us able to write right program. That's why I am asking this question on this forum. Any one know how to write Program to calculate days difference between two dates in java. Please help me.
    Thanks in advanced.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Program to calculate days difference between two dates in java.

    In the following example we are going to calculate the differences between two dates. For this you have to first converts the actual date value into the corresponding date / time value in milliseconds and then do the math so we can get the different in days, hours, minutes, etc.



    Code:
    package org.kodejava.example.util;
     
    import java.util.Calendar;
     
    public class DD
    {   
        public static void main(String[] args)
        {
           
           Calendar c1 = Calendar.getInstance();
           Calendar c2 = Calendar.getInstance();
             
               c1.set(2006, 12, 30);
            c2.set(2007, 5, 3);
     
            long milis1 = c1.getTimeInMillis();
            long milis2 = c2.getTimeInMillis();
    
            long d = milis2 - milis1;
             
            long dSeconds = diff / 1000;
             
     
            long dM = d / (60 * 1000);
           
            long dH = d / (60 * 60 * 1000);
    
            long dD = d / (24 * 60 * 60 * 1000);
             
            System.out.println("In milliseconds: " + d + " milliseconds.");
            System.out.println("In seconds: " + dS + " seconds.");
            System.out.println("In minutes: " + dM + " minutes.");
            System.out.println("In hours: " + dH + " hours.");
            System.out.println("In days: " + dD + " days.");
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Program to calculate days difference between two dates in java.

    You have to use setTimeInMillis(long millis) method to set current time in calendar object and you have to use getInstance() method to get a calendar using the default time zone, locale and current time.

    Code:
    import java.util.Calendar;
     
    public class DateDifferent{    
        public static void main(String[] args){
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.set(2010, 01, 21);
        ca2.set(2010, 10, 21);
        long m1 = calendar1.getTimeInMillis();
        long m2 = calendar2.getTimeInMillis();
        long d = m2 - m1;
        long dS = d / 1000;
        long dM = d / (60 * 1000);
        long dH = d / (60 * 60 * 1000);
        long dD = d / (24 * 60 * 60 * 1000);
        System.out.println("\nThe Date Different Example");
        System.out.println("Time in milliseconds: " + d
     + " m.");
        System.out.println("Time in seconds: " + dS
     + " seconds.");
        System.out.println("Time in minutes: " + dM 
    + " minutes.");
        System.out.println("Time in hours: " + dH
    + " hours.");
        System.out.println("Time in days: " + dD 
    + " days.");
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Program to calculate days difference between two dates in java.

    Hey it very easy process to calculate days difference between two days. I had written one example of it. Just go through it.

    Code:
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    
    public class DateDifference {
      public static void main(String[] av) {
        
        Date da1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();
    
    
        Date today1 = new Date();
    
     
        long diff1 = today1.getTime() - da1.getTime();
    
        System.out.println("The year (up to " + today + ") is "
            + (diff1 / (1000 * 60 * 60 * 24)) + "  the days old.");
      }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Program to calculate days difference between two dates in java.

    Hey you have to use following code in your program to calculate days difference between two days.

    Code:
       
     public static long daysBetween(Calendar sDate, Calendar eDate) {  
       Calendar d = (Calendar) sDate.clone();  
      long dBetween = 0;  
       while (d.before(eDate)) {  
         d.add(Calendar.DAY_OF_MONTH, 1);  
         dBetween++;  
       }  
       return dBetween;  
     }  
      12. }

  6. #6
    Join Date
    Mar 2011
    Posts
    1

    Re: Program to calculate days difference between two dates in java.

    Lol! It is blatantly obvious that you are a UOM student. I find it hard to believe that final year students have hardly touched java, even out of pure interest in programming. A search on google heads plenty of results which do exactly what you want as well.

Similar Threads

  1. How to Calculate Expiration Dates in Excel
    By Kungfu Pandey in forum Windows Software
    Replies: 1
    Last Post: 07-01-2012, 04:22 PM
  2. Auto calculate the amount of stays according to selected dates
    By Macario in forum Software Development
    Replies: 4
    Last Post: 17-11-2009, 10:13 PM
  3. Calculating difference in days between two dates in Asp.Net
    By Jagriti in forum Software Development
    Replies: 3
    Last Post: 07-07-2009, 11:23 PM
  4. How to calculate two dates in Excel
    By Ektaa in forum Windows Software
    Replies: 5
    Last Post: 03-03-2009, 05:50 PM
  5. How to calculate duration in days
    By aileen in forum Microsoft Project
    Replies: 3
    Last Post: 26-09-2008, 08:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,675,093,824.38227 seconds with 17 queries