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, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
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)

hacker-h
Guest
hacker-h

this way it will work:
DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”);
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //use .getTime() here

GregT
Guest
GregT

Above correction in comments is correct but example is not updated yet, you should update it.

Sachith
Guest
Sachith

I want to get current date and time using internet.

john
Guest
john

How to the retrieve the server time and not the local one using a java function?

rohmat
Guest
rohmat

Thanks ,

Yashwanth
Guest
Yashwanth

I am getting today data as nth day of this year as “136” instead of “16”.

Bob
Guest
Bob

I think your wanting day of month instead of day of year

Bin Mulyadin
Guest
Bin Mulyadin

it works! thank you

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()));

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

mkyong
Guest
mkyong

Thanks, article is updated.

Eugene Kartoyev
Guest
Eugene Kartoyev

Mmmm… There is no such word as “calendER” in English…. You need to correct the heading.

mkyong
Guest
mkyong

Article is updated, sorry for the typo, fixed.

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();

Miguel Enriquez
Guest
Miguel Enriquez

Hi friends how to display the hour AM/ PM? thanks

mkyong
Guest
mkyong

Append ‘a’ to represent am/pm, for example “yyyy/MM/dd HH:mm:ss a”

http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Amulya
Guest
Amulya

Thanks :)

Damir Olejar
Guest
Damir Olejar

NOTE: make sure Date.util is imported not the sql lib

Nan.m
Guest
Nan.m

how to check if date1 is 5-days behind date2 (Current Date)

Anup
Guest
Anup

Thanks for example.
How do I add five business days(Mon-Fri) to current date and get modifiedDate

cc69cc
Guest
cc69cc

Thank

Haroon Mind
Guest
Haroon Mind

Thanks it easy to understand !

er
Guest
er

Thx for the example, would be awesome if you could add the output of the system.out to the post

Raza
Guest
Raza

Thanks man , helped and worked

X1962X
Guest
X1962X

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

Mukesh Otwani
Guest
Mukesh Otwani

Hi Its worked with latest Java version I have tried the same.

You can check from your side as well. http://learn-automation.com/free-selenium-webdriver-tutorial/

allanruin
Guest
allanruin

You blog is really really helpful! I have found serveral articles that directly solve my problems, Thank you!

Good Guy
Guest
Good Guy

well, you could donate then ;-)

Muhammmad Raza
Guest
Muhammmad Raza

thanks

charles
Guest
charles

tnx a lot!!!;;;;;

xyz
Guest
xyz

Thanks…

edo
Guest
edo

It’s really help 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

Crina
Guest
Crina

Thanks a lot! It really helped me!

saf1
Guest
saf1

Thank you for the clarification.
Simple and precise.

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(); […]

stuart
Guest
stuart

I combined them:

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

stuart
Guest
stuart

PS, why does java have to be so obtuse?