Display four-digit year in Java

Java 8Object Oriented ProgrammingProgramming

Use the ‘Y’ date conversion character to display four-digit year.

System.out.printf("Four-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);
   }
}

Output

Current date and time = 26/11/2018 11:56:26 AM
Four-digit Year = 2018
raja
Published on 27-Dec-2018 10:33:56
Advertisements