Format Calendar with String.format() in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 14-Dec-2018 15:59:43
Advertisements