sexta-feira, julho 09, 2004

datas

How to calculate date diferences.
 
import
java.util.Calendar;
import java.util.Date;

/*
* Filename: Test.java
* Creation date: 14-mar-2004
* Author: Kevin Pors
*/


/**
*
@author Kevin Pors
* @version 1.0
*/

public class Test
{
    public static void main(String[] args) {
        Date
now = new Date(); // right now.

      
Calendar c = Calendar.getInstance();
        c.setTime(now);
      
c.add(Calendar.HOUR, 1092);

        Date someDate = c.getTime();

  
    getDaysBetween(now, someDate);
    }

    public static
int getDaysBetween(Date dateOne, Date dateTwo) {
        long
milliseconds = dateTwo.getTime() - dateOne.getTime();
        if (milliseconds
< 0) {
            milliseconds = -milliseconds;
        }
       
        long seconds = milliseconds / 1000L;
        long minutes =
seconds / 60L;
        long hours = minutes / 60L;
        long
days = hours / 24L;

        System.out.println("Difference in milliseconds: " + milliseconds);
   
   System.out.println("Difference in seconds: " +
seconds);
        System.out.println("Difference in
minutes: "
+ minutes);
        System.out.println("Difference in hours: " + hours);
      
System.out.println("Difference in days: " +
days);

        return 0;
    }
}

Nenhum comentário: