Java -Retrieve current date and time

July 2017




Issue


I need to display JFrame with labels and next to these labels, the date and time at which they were typed.
For this I need to retrieve and display the date and time in a JLabel but I can not even recover those below in a simple format such as: DD/MM/YYYY and HH/ MM/SS.

Solution


You must use the Date class from the java.util package
Inside, you have the Date () constructor which creates the current date. To obtain a more readable form you do a:

Date myDate = new Date();  
// Display:  
System.out.println(myDate.toString());


Thanks to maily for this tip.

Related


Published by deri58. Latest update on September 20, 2012 at 10:27 AM by deri58.
This document, titled "Java -Retrieve current date and time," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).