Display Two Digit Year in Java



Use the ‘y’ date conversion character to display two-digit year.

System.out.printf("Two-digit Year = %TY\n",d);

Above, d is a date object −

Date d = new Date();

The following is an example −

Example

 Live Demo

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
public class Demo {
   public static void main(String[] args) throws Exception {
      Date d = new Date();
      DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");
      String format = dateFormat.format(d);
      System.out.println("Current date and time = " + format);
      System.out.printf("Four-digit Year = %TY\n",d);
      System.out.printf("Two-digit Year = %ty\n",d);
   }
}

Output

Current date and time = 26/11/2018 12:00:51 PM
Four-digit Year = 2018
Two-digit Year = 18
Updated on: 2020-06-27T14:51:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements