Format Calendar with String.format() in Java


Firstly, consider an object with date value.

Object arrObj[] = { "Date", Calendar.getInstance() };

After that, use the String.format() method to format Calendar and display the date.

The following is an example.

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String []args){
      Object arrObj[] = { "Date", Calendar.getInstance() };
      System.out.println("Formatting Date...");
      System.out.println(String.format("%1$s = %2$tY %2$tm %2$te", arrObj));
   }
}

Output

Formatting Date...
Date = 2018 11 17

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

605 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements