By using SimpleDateFormat and Date/Calendar
class, we can easily get current date and time in Java. In this tutorial we will see how to get the current date and time using Date and Calendar class and how to get it in the desired format using SimpleDateFormat class.
Current date and time in Java – Two ways to get it
1) Using Date class
- Specify the desired pattern while creating the instance of
SimpleDateFormat
. - Create an object of Date class.
- Call the format() method of DateFormat class and pass the date object as a parameter to the method.
/* This will display the date and time in the format of * 12/09/2017 24:12:35. See the complete program below */ DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss"); Date dateobj = new Date(); System.out.println(df.format(dateobj));
2) Using Calendar class
- Specify the desired pattern for the date and time. Similar to the step 1 of above method.
- Create an object of
Calendar
class by callinggetInstance()
method of it. - Call the
format()
method of DateFormat and pass theCalendar.getTime()
as a parameter to the method.
DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss"); Calendar calobj = Calendar.getInstance(); System.out.println(df.format(calobj.getTime()));
Complete java code for getting current date and time
import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; public class GettingCurrentDate { public static void main(String[] args) { //getting current date and time using Date class DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss"); Date dateobj = new Date(); System.out.println(df.format(dateobj)); /*getting current date time using calendar class * An Alternative of above*/ Calendar calobj = Calendar.getInstance(); System.out.println(df.format(calobj.getTime())); } }
Output:
21/10/17 22:13:06 21/10/17 22:13:06
Every time I run the above code it would fetch the current date and time.
Note: In order to get the output in above format I have specified the date/time pattern in the program (Note the first statement of the program DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
.
However if you want the output in any other date format, just modify the pattern accordingly. For e.g. To get the date only, the pattern would be dd-MM-yyyy: replace the statement with this one: DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
While specifying the pattern be careful with the case. For e.g. ‘s’ (small s) represents second while ‘S'(Capital s) represents Millisecond.
At the end of this guide, I have shared the complete chart of symbols that we can use in patterns to get the date and time in desired format
Java – Getting current date and time in other timezone
The example we have seen above shows the date and time in local timezone. However we can get the date and time in different time zone as well such as UTC/GMT etc. In the following example we are displaying the time in GMT time zone.
import java.util.Date; import java.text.SimpleDateFormat; import java.util.TimeZone; public class Example { public static void main(String[] args) { //"hh" in pattern is for 12 hour time format and "aa" is for AM/PM SimpleDateFormat dateTimeInGMT = new SimpleDateFormat("yyyy-MMM-dd hh:mm:ss aa"); //Setting the time zone dateTimeInGMT.setTimeZone(TimeZone.getTimeZone("GMT")); System.out.println(dateTimeInGMT.format(new Date())); } }
Output:
2017-Oct-21 06:03:42 PM
Update: To get the current date and time in Java 8 refer this guide.
Here is the complete chart which will help you to define the pattern in SimpleDateFormat
.
Letter | Date or Time Component | Presentation | Examples |
---|---|---|---|
G |
Era designator | Text | AD |
y |
Year | Year | 1996 ; 96 |
Y |
Week year | Year | 2009 ; 09 |
M |
Month in year | Month | July ; Jul ; 07 |
w |
Week in year | Number | 27 |
W |
Week in month | Number | 2 |
D |
Day in year | Number | 189 |
d |
Day in month | Number | 10 |
F |
Day of week in month | Number | 2 |
E |
Day name in week | Text | Tuesday ; Tue |
u |
Day number of week (1 = Monday, …, 7 = Sunday) | Number | 1 |
a |
Am/pm marker | Text | PM |
H |
Hour in day (0-23) | Number | 0 |
k |
Hour in day (1-24) | Number | 24 |
K |
Hour in am/pm (0-11) | Number | 0 |
h |
Hour in am/pm (1-12) | Number | 12 |
m |
Minute in hour | Number | 30 |
s |
Second in minute | Number | 55 |
S |
Millisecond | Number | 978 |
z |
Time zone | General time zone | Pacific Standard Time ; PST ; GMT-08:00 |
Z |
Time zone | RFC 822 time zone | -0800 |
X |
Time zone | ISO 8601 time zone | -08 ; -0800 ; -08:00 |
Leave a Reply