Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A java program to get current date time using the java.util.Date class. Check the blog post at https://coderolls.com/get-current-date-time-in-java/
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* A java program to get current date time using the java.util.Date class
* @author Gaurav Kukade at coderolls.com
*
*/
public class DateExample {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
// print date object
System.out.println(date); // Sat May 15 12:21:39 IST 2021
// print formated date object
System.out.println(simpleDateFormat.format(date)); // 15/05/2021 12:21:39
// creates new Date() object using System.currentTimeMillis()
Date date2 = new Date(System.currentTimeMillis());
System.out.println(simpleDateFormat.format(date)); // 15/05/2021 12:21:39
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment