java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4092
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Compute days between 2 dates E-mail
User Rating: / 26
PoorBest 

This example shows how to compute days between 2 dates.

import java.util.*;

public class TestDate {
  public static void main(String args[]){
    /*
     ex . 
       java TestDate 1999 0 1 1999 8 1
           to get days between 1999-01-01 and 1999-09-01
          (remember months are zero-based...)
   */
  
    TestDate a = new TestDate(args);
    }

  TestDate(String args[]) {
    Calendar c1 = new GregorianCalendar();;
    Calendar c2 = new GregorianCalendar();;

    //  need more error checking here...
    c1.set(Integer.parseInt(args[0]),
           Integer.parseInt(args[1]) 
           Integer.parseInt(args[2])000)
    c2.set(Integer.parseInt(args[3]),
          Integer.parseInt(args[4]) 
          Integer.parseInt(args[5])000)

    System.out.println
       (daysBetween(c1.getTime(),c2.getTime()) +
        " day(s) between " + args[0"-" + args[1"-" + args[2+
        " and " + args[3"-" + args[4"-" + args[5]);
    }

  static final long ONE_HOUR = 60 60 1000L;
  public long daysBetween(Date d1, Date d2){
    return ( (d2.getTime() - d1.getTime() + ONE_HOUR
                  (ONE_HOUR * 24));
     }   
  }

 Related Tips

 
< Prev   Next >
 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.