Display two-digit month in Java

Use the ‘m’ date conversion character to display two-digit month.

System.out.printf("Two-digit month: %tm

Above, d is a date object −

Date d = new Date();

The following is an 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
",d);       System.out.printf("Two-digit Year = %ty
",d);       System.out.printf("Three-digit Day of the Year: %tj/%Tj
", d, d);       System.out.printf("Two-digit month: %tm
",d);    } }


Current date and time = 26/11/2018 12:18:51 PM
Four-digit Year = 2018
Two-digit Year = 18
Three-digit Day of the Year: 330/330
Two-digit month: 11

Updated on: 27-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started