Java – How to get current date time

In this tutorial, we will show you how to get the current date time from the classic Date and Calendar APIs, and also the new Java 8 date and time APIs – LocalDateTime and LocalDate

1. Code Snippets

For java.util.Date, just create a new Date()


	DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	Date date = new Date();
	System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43

For java.util.Calendar, uses Calendar.getInstance()


	DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	Calendar cal = Calendar.getInstance();
	System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43

For java.time.LocalDateTime, uses LocalDateTime.now()


	DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
	LocalDateTime now = LocalDateTime.now();
	System.out.println(dtf.format(now)); //2016/11/16 12:08:43

For java.time.LocalDate, uses LocalDate.now()


	DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
	LocalDate localDate = LocalDate.now();
	System.out.println(dtf.format(localDate)); //2016/11/16

2. Full Example

Review a full Java example to show you how to get the current date, time and display in a predefined format.

GetCurrentDateTime.java

package com.mkyong;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class GetCurrentDateTime {

    private static final DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private static final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

    public static void main(String[] args) {

        Date date = new Date();
        System.out.println(sdf.format(date));

        Calendar cal = Calendar.getInstance();
        System.out.println(sdf.format(cal.getTime()));

        LocalDateTime now = LocalDateTime.now();
        System.out.println(dtf.format(now));

        LocalDate localDate = LocalDate.now();
        System.out.println(DateTimeFormatter.ofPattern("yyy/MM/dd").format(localDate));

    }

}

Output


2016/11/16 12:08:43
2016/11/16 12:08:43
2016/11/16 12:08:43
2016/11/16

References

  1. Date JavaDoc
  2. Calendar JavaDoc
  3. SimpleDateFormat JavaDoc
  4. LocalDateTime JavaDoc
  5. LocalDate JavaDoc
  6. DateTimeFormatter JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
63 Comment threads
18 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
68 Comment authors
ANKIT DONDAimethGuestBabu gMiguel Recent comment authors
newest oldest most voted
Younis Irshad
Guest
Younis Irshad

In Customer CLASS Use to get : Customer [date=Thu Aug 13 11:42:06 EDT 2015]

Date date = new Date();

Ran Cohen
Guest
Ran Cohen

Bug in java 8:
DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43

Throws Exception in thread “main” java.lang.IllegalArgumentException: Cannot format given Object as a Date
at java.text.DateFormat.format(DateFormat.java:310)
at java.text.Format.format(Format.java:157)
at com.mains.SearchStringInAllFiles.main(SearchStringInAllFiles.java:30)

Serhii Hrabas
Guest
Serhii Hrabas

DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(date)); //2016/11/16 12:08:43

There is a mistake here, should be
System.out.println(dateFormat.format(cal)); //2016/11/16 12:08:43

edo
Guest
edo

It’s really help me

X1962X
Guest
X1962X

This doesn’t work with the latest Java updates. I was hoping it would work, but doesn’t.

Aditya
Guest
Aditya

DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal));

Above code not working – getting illigalarguement exception. Following is working for me.

DateFormat dateFormat = new SimpleDateFormat(“yy/mm/dd-hh::mm”);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE,1);
System.out.println(dateFormat.format(cal.getTime()));

mjb
Guest
mjb

1. Calendar.getInstance() is noticeably more expensive than new Date(). Not that one shouldn’t use it – it has many important capabilities. But in this comparison, that’s relevant

2. SimpleDateFormat is not thread safe. This isn’t relevant here, but this bites so many people, I mention it by reflex

hhhhh
Guest
hhhhh

mjb says:
August 16, 2009 at 8:25 am

1. Calendar.getInstance() is noticeably more expensive than new Date(). Not that one shouldn’t use it – it has many important capabilities. But in this comparison, that’s relevant

2. SimpleDateFormat is not thread safe. This isn’t relevant here, but this bites so many people, I mention it by reflex

Ganga
Guest
Ganga

Very easy 2 understand…Highly useful for beginners! 🙂

jonah
Guest
jonah

thanks, helped me

Kristina
Guest
Kristina

Thank you 🙂

Cole Jackson
Guest
Cole Jackson

Hey, system time is really unreliable and can seriously mess up programs if incorrect. Is there a way to read the time from a website like this http://www.time.gov/widget.html
Or this
http://wwp.greenwichmeantime.com/time/scripts/clock-8/runner.php
instead?

Laras
Guest
Laras

how about get client’s date format…?

priya
Guest
priya

Is there any possibility to convert from string (01:30) to time .

actually in view i have a drop down to display time with 15 min interval
in backend(db2) i need to store as time.

anD
Guest
anD

Thank you.

Garry
Guest
Garry

Hi

Very useful I came across many tutorials But you explained in both the ways which was helpful.

Hassan
Guest
Hassan

thanks

Sagar
Guest
Sagar

sir i want to get date………. like six digit 110698 is entered by the user than show message 11 June 1998 date is valid if date is entered 231489 than show invalid date message. thanks

Angel
Guest
Angel

Omg, you have tutorials for everything its amazing; you are really saving me a lot of time; thank you for you very good how to and examples

John
Guest
John

Actaully, i need a program through which i can display the and and time of the time when the user is actually using it. Can u help me ?? Quick please.

Parth Suri
Guest
Parth Suri

Thanks a lot.

vanu
Guest
vanu

Thank you Sir.

Raj
Guest
Raj

I have an issue with Calendar.getInstance.getTime() , its working for all the time zones except CST.It is working well for EST,IST. I know its weird. The issue is its showing 1 hour less for CST than the actual.

Please advise, quickly appreciated

Raj
Guest
Raj

Hi,
Calendar.getInstance.getTime() is working fine except CST timezone, it is showing 1 hour less than the actual. Please help

Raj

Simone
Guest
Simone

Sir, you are a life saviour. Thanks.

stuart
Guest
stuart

I combined them:

System.out.println(“Oraloader Initialized: ” + new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”).format( Calendar.getInstance().getTime()));

trackback
Append to a File - How-To Video

[…] } /* replace this with your own username */ String user="kumar116"; w.append(user+"t"); /* c/p http://www.mkyong.com/java/java-how-t... */ DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date date = new Date(); […]

saf1
Guest
saf1

Thank you for the clarification.
Simple and precise.

Crina
Guest
Crina

Thanks a lot! It really helped me!

InnoRufi
Guest
InnoRufi

Sir i want to know how can i display time according to my country time zone..For GMT could u help me please