Search
Sponsors
Sponsors

Archive for the ‘Date and Time’ Category

Check for leap year

Thursday, August 25th, 2011

 

public boolean isLeapYear(){
    boolean leapYear;
    int divideByFour = year % 4;
    int divideBy100 = year % 100;
    int divideBy400 = year % 400;
    if (divideBy400 == 0){
        leapYear = true;
    } else if (divideBy100 == 0){
        leapYear = false;
    } else if (divideByFour == 0){
        leapYear = true;
    } else {
        leapYear = false;
    }
    return leapYear;
}

Past and future dates

Tuesday, February 24th, 2009

/**
 * @(#)test.java
 * test application
 */
import java.util.Date;
public class test
{
   
    public static void main(String[] args)
    {
 Date now = new Date();
 
    long past = now.getTime();
    long future = now.getTime();
    past -= 300 * 24 * 60 * 60 * 1000;
 future += 300 * 24 * 60 * 60 * 1000;
 Date pastdate = new Date(past);
 Date futuredate = new Date(future);
    System.out.println(”300 days ago was ” + pastdate);
 System.out.println(”300 days from now will be ” + futuredate);
    }

}

On my system

300 days ago was Sun Feb 22 17:51:41 GMT 2009
300 days from now will be Thu Feb 26 05:18:13 GMT 2009

Translate