Date

import java.util.Date;

Arithmetic

final Calendar calendar = new GregorianCalendar(2009, Calendar.JANUARY, 22);
calendar.add(Calendar.MONTH, 7);

Calendar

Get the number of days in a month:

import java.util.GregorianCalendar;
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTime(invoice.getDueDate());
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH)

Gets the date and time now!

java.util.Date date = new java.util.Date();
out.write(date.toString());

Construct a date:

Working in Java time Learn the basics of calculating elapsed time in Java.

/*
 * Note.  Months are zero based.  So Jan == 0.
 * Use the constants in the calendar class to make things easier.
 */
Calendar c = new GregorianCalendar(1964, Calendar.SEPTEMBER, 22);
Date d = c.getTime();
// Also...
// Calendar c = new GregorianCalendar();
// GregorianCalendar(int year, int month, int date, int hour, int minute, int second)

Milliseconds

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object

new Date().getTime()

String

How do I convert String to Date object?

package org.kodejava.example.java.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.Date;

public class StringToDate
{
    public static void main(String[] args)
    {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

        try
        {
            Date today = df.parse("20/12/2005");
            System.out.println("Today = " + df.format(today));
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
    }
}

ToString

SimpleDateFormat:

Convert a date to a formatted string:

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
// another example...        new SimpleDateFormat("yyyyMMdd_kkmmss_SSSS")
return formatter.format(fieldDate);

Letter

Component

Example

d

Day in month

10

k

Hour in the day (1-24)

24

m

Minute in the hour

30

M

Month in year

2

s

Seconds in a minute

55

S

Milliseconds

123

y

Year

1996

Difference

Days

Losing Time on the Garden Path: Calculating the Difference Between Two Datetime Stamps

The problem is that the obvious solution – subtracting the millisecond timestamps of two moments and dividing by the number of milliseconds per day, contains a bug

Example project with TimeHelper class:

http://g1/svn/home/patrick/learn/time-difference-example/

public final class TimeHelper {

    /** Time calculation. */
    private static final long MILLISECS_PER_MINUTE = 60 * 1000;
    /** Time calculation. */
    private static final long MILLISECS_PER_HOUR = 60 * MILLISECS_PER_MINUTE;
    /** Time calculation. */
    private static final long MILLISECS_PER_DAY = 24 * MILLISECS_PER_HOUR;

    public TimeHelper() { }

    /**
     * Compares two dates and returns the number of days between the two dates.
     */
    public static long differenceInDays(final Date startDate, final Date endDate) {
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(endDate);
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(startDate);
        long endL = endCal.getTimeInMillis() + endCal.getTimeZone().getOffset(endCal.getTimeInMillis());
        long startL = startCal.getTimeInMillis() + startCal.getTimeZone().getOffset(startCal.getTimeInMillis());
        return (endL - startL) / MILLISECS_PER_DAY;
    }
}