Java – How to get current date time – date() and calender()

In Java, you can get the current date time via following two classes – Date and Calendar. And, later use SimpleDateFormat class to convert the date into a user friendly format.

1. Date() + SimpleDateFormat()

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //2014/08/06 15:59:48

2. Calender() + SimpleDateFormat()

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime())); //2014/08/06 16:00:22

Review a full example to show you how to use Date() and Calender() classes to get and display the current date time.

GetCurrentDateTime.java
package com.mkyong;
 
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
public class GetCurrentDateTime {
  public static void main(String[] args) {
 
	   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	   //get current date time with Date()
	   Date date = new Date();
	   System.out.println(dateFormat.format(date));
 
	   //get current date time with Calendar()
	   Calendar cal = Calendar.getInstance();
	   System.out.println(dateFormat.format(cal.getTime()));
 
  }
}

Output

2014/08/06 16:06:54
2014/08/06 16:06:54

References

  1. Date JavaDoc
  2. Calendar JavaDoc
  3. SimpleDateFormat JavaDoc
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.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

  • Muhammmad Raza

    thanks

  • Pingback: Java – How to get current datetime | sandramilhais()

  • charles

    tnx a lot!!!;;;;;

  • xyz

    Thanks…

  • Harish

    Thank you it is helped me !!!!!!!!!!!!!

  • Harish

    Thank you
    It is helped me.

  • Andreas Gesos

    Ohh I love you!!! :) I love your tutorials!!!

  • http://edokode.com edo

    It’s really help me

  • 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

    Thanks a lot! It really helped me!

  • saf1

    Thank you for the clarification.
    Simple and precise.

  • Pingback: Append to a File - How-To Video()

  • stuart

    I combined them:

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

    • stuart

      PS, why does java have to be so obtuse?

  • http://simonewebdesign.it Simone

    Sir, you are a life saviour. Thanks.

  • http://gmail.com Raj

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

    Raj

  • 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

  • vanu

    Thank you Sir.

  • Parth Suri

    Thanks a lot.

  • 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.

  • 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

  • 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

  • Hassan

    thanks

  • http://[email protected] Garry

    Hi

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

  • anD

    Thank you.

  • 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.

  • Laras

    how about get client’s date format…?

  • http://twitter.com/#!/salvegames 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?

  • Kristina

    Thank you :)

  • jonah

    thanks, helped me

  • Ganga

    Very easy 2 understand…Highly useful for beginners! :)

  • 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

    • b1naryatr0phy

      “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”

      And on what do you base that opinion? Not only is nearly every method in Date() deprecated (and soon the class will be as well,) but even the Java architects suggest using Calendar() over Date()

      On a side note, I prefer getting the date/time with a single line of code:
      Calendar.getInstance().getTime();

      • http://www.mkyong.com mkyong

        Just show you two ways of getting current date and time, not comparison :)

      • AGrownUp

        b1naryatr0phy:

        Well aren’t you just the most wonderful person ever! I thank the good Lord every day for people like you – you’re so helpful to have around, especially responding 3 years later.

        Here’s some guy trying to help some other people out and some db comes along and dumps on him for it. If you know all this stuff, why are you here?

        • Chinpokomon

          I didn’t see anything wrong with providing that information. I was actually wondering which would be better myself and I’m glad he responded the way he did. I appreciate the original posting as it steered me in the correct direction and b1naryatr0phy’s comment filled in the gap in my knowledge. Thanks to the mkyong and b1naryatr0phy.

          • Chinpokomon

            Or rather I guess it was hhhhh for initially discussing the considerations of both methods. As I see it, more information is always better if it adds to the conversation.

          • Jules

            I do: it’s completely wrong!

            I was recently assigned to refactor some code looking for bottlenecks, and guess what? Several times per second the new Date() was being called in a log function, and in traceview it would seriously show up! So I changed new Date() to Calendar.getInstance().getTime() and it really made a difference in traceview. I will not bother posting screens and logs, but try reading Google’s Android Performance papers, or any senior Java consultant: they all say that object creation is very expensive reagarding accessing a singleton.

  • 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