Display Two Digit Month in Java



In this article, we will discuss how to display the two-digit month. A two-digit month ensures that even the first month (i.e., January) is formatted to display as 01 instead of 1. This formatting provides a consistent two-digit representation for all months.

To Display two-digit month in Java is quite easy. Let us learn the following ways:

Displaying Two-Digit Month Using the "%tm" Specifier

To retrieve a two-digit month from a given date, we can use the "%tm" specifier. This format specifier is used with the printf() method to display the month of a date as a two-digit number.

Example

In the following example, we use the %tm format specifier with printf() method to retrieve a two-digit month from the current date ?

import java.util.Date;

public class Main {
   public static void main(String[] args) {
      Date date = new Date();       
      System.out.printf("Two-digit month: %tm%n", date);
   }
}

Output

Following is the output of the above program ?

Two-digit month: 12

Using SimpleDateFormat Class

Another way to get the two-digit month is by formatting the given date using the SimpleDateFormat Class. The SimpleDateFormat class is a formatter that belongs to java.text package it formatted the passed date according to the specified pattern.

Example

In this example we use the SimpleDateFormat Class to format and retrieve the two-digit month from the current date ?

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

public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        String month = sdf.format(date);
        System.out.printf("Two-digit month: %s%n", month);
    }
}

Output

The above program produces the following output ?

Two-digit month: 12

Using String.format() Method

We have another way to format and retrieve the two-digit month using String.format() method. In Java, this method allows you to format numbers, dates, and other types of data into a string.

Example

In the example below, we use the String.format() method to retrieve the two-digit month from the given date ?

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

public class Main {
    public static void main(String[] args) {
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy");
            Date date = inputFormat.parse("20/11/2024");//custom date
            int onlyMonth = date.getMonth()+1;
            String month = String.format("%02d", onlyMonth);
            System.out.println("Two-digit month: " + month);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Output

The above program displays the following output ?

Two-digit month: 11
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2024-12-23T11:15:55+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements