Display Complete Date and Time Information Using Formatter in Java



Firstly, create a Formatter and Calendar object.

Formatter f = new Formatter();
Calendar cal = Calendar.getInstance();

Now display the current date and time. We have shown the date here in both lowercase and uppercase −

f = new Formatter();
System.out.println(f.format("\nDate and Time (lowercase): %tc\n", cal));
f = new Formatter();
System.out.println(f.format("Date and Time (uppercase): %Tc\n", cal));

The following is an example −

Example

 Live Demo

import java.util.Calendar;
import java.util.Formatter;
public class Demo {
   public static void main(String args[]) {
      Formatter f = new Formatter();
      Calendar cal = Calendar.getInstance();
      System.out.println("Current date and time: "+cal.getTime());
      f = new Formatter();
      System.out.println(f.format("\nDate and Time (lowercase): %tc\n", cal));
      f = new Formatter();
      System.out.println(f.format("Date and Time (uppercase): %Tc\n", cal));
   }
}

Output

Current date and time: Mon Nov 26 07:24:21 UTC 2018

Date and Time (lowercase): Mon Nov 26 07:24:21 UTC 2018

Date and Time (uppercase): MON NOV 26 07:24:21 UTC 2018
Updated on: 2019-07-30T22:30:24+05:30

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements