Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

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.

Pictorial Presentation:

Java String Exercises: 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.

Flowchart:

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

Visualize Java code execution (Python Tutor):


Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to compare a given string to another string, ignoring case considerations.
Next: Write a Java program to get the contents of a given string as a byte array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Java: Tips of the Day

How to sort an ArrayList?

Collections.sort(testList);
Collections.reverse(testList);

That will do what you want. Remember to import Collections though!

Ref: https://bit.ly/32urdSe