How to Format Year Using Java SimpleDateFormat


Here we will learn about how to format year with SimpleDateFormat class in Java. As we known, year may be formatted in either YY or YYYY formats.

Source Code

package com.beginner.examples;

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

public class FormattingYear {

	public static void main(String[] args) {
		//create Date.
		Date date = new Date();
		
		/*
		 * To format year in yy formate use SimpleDateFormat("yy") Constructor.
		 */
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yy");
		
		System.out.println("Current year in yy format is : " + simpleDateFormat1.format(date));
		
		/*
		 * To format year in yyyy formate use SimpleDateFormat("yyyy") Constructor.
		 */
		SimpleDateFormat SimpleDateFormat2 = new SimpleDateFormat("yyyy");
		
		System.out.println("Current year in yyyy format is : " + SimpleDateFormat2.format(date));

	}

}

Output:

Current year in yy format is : 19
Current year in yyyy format is : 2019

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments