Display first two digits of year in Java (two-digit century)


Use the ‘C’ date conversion character to display two digits of year −

System.out.printf("Two-digit Year (Century Name) = %tC/%TC
", d, 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("Localized day name = %tA/%TA
", d, d);       System.out.printf("Two-digit Year (Century Name) = %tC/%TC
", d, d);    } }

Output

Current date and time = 26/11/2018 11:51:18 AM
Localized day name = Monday/MONDAY
Two-digit Year (Century Name) = 20/20

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 27-Jun-2020

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements