Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created May 15, 2021 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravkukade/c0abb86106b161cd9b3d12de37e34cb3 to your computer and use it in GitHub Desktop.
Save gauravkukade/c0abb86106b161cd9b3d12de37e34cb3 to your computer and use it in GitHub Desktop.
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