In this article, we will learn how to get current Date in different ways
Get Current Date :
- Using Date object
- Using Calendar instance
1. Using Date object :
- Instantiating Date class returns current Date/Time in the default (EEE MMM dd HH:mm:ss zzz yyyy) format
- We can convert default format to custom format using DateFormat/SimpleDateFormat classes
CurrentDateTimeInJavaUsingDate.java
package in.bench.resources.java8.current.date.time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CurrentDateTimeInJavaUsingDate {
public static void main(String[] args) {
// 1. current date
Date date = new Date();
System.out.println("Current Date/Time"
+ " using Date object :- \n" + date);
// 1.1 format
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String str = dateFormat.format(date);
System.out.println("\nFormatted Date/Time in dd-MM-yyyy HH:mm:ss format"
+ " using Date() :- \n" + str);
// 2. current date
Date date2 = new Date(System.currentTimeMillis());
System.out.println("\n\nCurrent Date/Time"
+ " using Date(System.currentTimeMillis()) :- \n" + date2);
// 2.1 format
DateFormat dateFormat2 = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
str = dateFormat2.format(date2);
System.out.print("\nFormatted Date/Time in dd.MM.yyyy HH:mm:ss format"
+ " using Date(System.currentTimeMillis()) :- \n" + str);
}
}
Output:
Current Date/Time using Date object :-
Mon Sep 12 16:02:22 IST 2022
Formatted Date/Time in dd-MM-yyyy HH:mm:ss format using Date() :-
12-09-2022 16:02:22
Current Date/Time using Date(System.currentTimeMillis()) :-
Mon Sep 12 16:02:22 IST 2022
Formatted Date/Time in dd.MM.yyyy HH:mm:ss format using Date(System.currentTimeMillis()) :-
12.09.2022 16:02:22
2. Using Calendar instance :
- Calendar.getInstance() method returns Calendar instance which has many useful methods and one such method is getTime() which returns current System Date/Time in the default (EEE MMM dd HH:mm:ss zzz yyyy) format
- We can convert default format to custom format using DateFormat/SimpleDateFormat classes
CurrentDateTimeInJavaUsingCalendar.java
package in.bench.resources.java8.current.date.time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class CurrentDateTimeInJavaUsingCalendar {
public static void main(String[] args) {
// 1. current date
Calendar calendar = Calendar.getInstance();
System.out.println("Current Date/Time"
+ " using Calendar :- \n" + calendar.getTime());
// 2. format dd-MM-yyyy HH:mm:ss
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String str = dateFormat.format(calendar.getTime());
System.out.print("\nFormatted Date/Time"
+ " using Calendar :- \n" + str);
}
}
Output:
Current Date/Time using Calendar :-
Mon Sep 12 16:02:38 IST 2022
Formatted Date/Time using Calendar :-
12/09/2022 16:02:38
Related Articles:
- Java 8 – How to get current Date in different ways ?
- Java 8 – How to convert java.util.Date to LocalDate and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to OffsetDateTime and vice-versa ?
- Java 8 – How to convert java.util.Date to Instant and vice-versa ?
- Java 8 – How to convert java.util.Date to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert java.util.Date to Calendar and vice-versa ?
- Java 8 – How to convert java.util.Date to GregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to XMLGregorianCalendar and vice-versa ?
- Java 9 – How to convert java.util.Date to LocalDate using ofInstant() method ?
- Java 9 – How to convert java.util.Date to LocalTime using ofInstant() method ?
- Java – How to convert java.util.Date to String in different formats ?
- Java – How to convert String to java.util.Date in different formats ?
- Java – How to get Date/time with AM/PM marker and Zone ?
- Java 8 – How to get all dates between two Dates ?
References:
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html
- https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Happy Coding !!
Happy Learning !!