Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created May 15, 2021 11:11
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/b29079c6b840d9c894c836468d9326dd to your computer and use it in GitHub Desktop.
Save gauravkukade/b29079c6b840d9c894c836468d9326dd to your computer and use it in GitHub Desktop.
A java program to get current date time using the java.util.Calendar class. Check the blog post at https://coderolls.com/get-current-date-time-in-java/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* A java program to get current date time using the java.util.Calendar class
* @author Gaurav Kukade at coderolls.com
*/
public class CalendarExample {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// get the instance of the java.util.Calendar
Calendar calendar = Calendar.getInstance();
// getTime() method returns the instance of java.util.Date
Date date = calendar.getTime();
// prints the date object
System.out.println(date); // Sat May 15 12:50:17 IST 2021
// print the formated date
System.out.println(simpleDateFormat.format(date)); // 15/05/2021 12:50:17
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment