w3resource logo

:

Java Programming Exercies

Java String Exercises: Print current date and time in the specified format

Java String : Exercise-15 with Solution

Write a java program to print current date and time in the specified format.

Sample Solution:

Java Code:

import java.util.Calendar;

public class Exercise15 {
 
 public static void main(String[] args) { 
    Calendar c = Calendar.getInstance();
      System.out.println("Current Date and Time :"); 
      System.out.format("%tB %te, %tY%n", c, c, c);
      System.out.format("%tl:%tM %tp%n", c, c, c); 
   }
   }

Sample Output:

Current Date and Time :                                                                                       
June 19, 2017                                                                                                 
3:13 pm 

N.B. : The current date and time will change according to your system date and time.

Java Code Editor:

Improve this sample solution and post your code through Disqus