w3resource logo


Java Programming Exercies

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

Secondary Nav

Java String : Exercise-15 with Solution

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

Sample Output :

Current Date and Time :                                                                                       
September 10, 2016                                                                                            
4:26 pm 

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); 
   }
   }

Output:

Current Date and Time :                                                 
September 10, 2016                                                      
4:55 pm

Java Practice online:

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); 
   }
   }



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program