SimpleDateFormat Year Example in java

This example shows you how to get the Current year from a java date object using SimpleDateFormat object.

package com.java.connect.date;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
public class SimpleDateFormatYearExample {
    public static void main(String[] args) {
        // Get the simple date format object by passing the format in string
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        // get the date in String format by passing current date
        // in format method of simple date format object
        Calendar originalDate = Calendar.getInstance();
        System.out.println("The original date is : " + originalDate.getTime());
        String dateString = format.format(originalDate.getTime());
        // Print the formated date.
        System.out.println("The Current year is : " + dateString);
    }
}

The output of above example will be as:

The original date is : Sun May 08 06:32:30 GMT 2011
The Current year is : 2011