How do I check if a year is a leap year?

package org.kodejava.example.util;

import java.util.GregorianCalendar;

public class LeapYearExample {
    public static void main(String[] args) {
        // Here we show how to know if a specified year is a leap year or 
        // not. The GregorianCalendar object provide a convenient method 
        // to do this. The method is GregorianCalendar.isLeapYear().

        // First, let's obtain an instance of GregorianCalendar.
        GregorianCalendar cal = new GregorianCalendar();

        // The isLeapYear(int year) method will return true for leap 
        // year and otherwise return false. In this example the message 
        // will be printed as 2004 is a leap year.
        if (cal.isLeapYear(2004)) {
            System.out.println("The year 2004 is a leap year!");

The result of our code is:

The year 2004 is a leap year!

How do I get date, month, year values from the current date?

What date, month, year, day-of-week, day-of-month, day-of-year is it today? If we want to answer these question we can use java.util.Calendar and java.util.GregorianCalendar which is the implementation of Calendar abstract class.

These classes can help us to get integer value such as date, month, year from a Date object. Let’s see the example code.

package org.kodejava.example.util;

import java.util.Calendar;

public class CalendarExample {
    public static void main(String[] args) {
        // Get various information from the Date object.
        Calendar cal = Calendar.getInstance();
        int day = cal.get(Calendar.DATE);
        int month = cal.get(Calendar.MONTH) + 1;
        int year = cal.get(Calendar.YEAR);
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        int doy = cal.get(Calendar.DAY_OF_YEAR);

        System.out.println("Current Date: " + cal.getTime());
        System.out.println("Day         : " + day);
        System.out.println("Month       : " + month);
        System.out.println("Year        : " + year);
        System.out.println("Day of Week : " + dow);
        System.out.println("Day of Month: " + dom);
        System.out.println("Day of Year : " + doy);

Here is the result of this example:

Current Date: Sat Feb 06 19:58:42 WITA 2016
Day         : 6
Month       : 2
Year        : 2016
Day of Week : 7
Day of Month: 6
Day of Year : 37

You might also want to try to use the Joda Time library for this. Here another example for getting information about date and time using Joda: How do I get date / time fields of date in Joda?.