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 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));
2. Calender() + SimpleDateFormat()
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); System.out.println(dateFormat.format(cal.getTime()));
Full example
Here is the full source code to show you the use of Date() and Calender() class to get and display the current date time.
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())); } }

Thank you for the clarification.
Simple and precise.
I combined them:
System.out.println(“Oraloader Initialized: ” + new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”).format( Calendar.getInstance().getTime()));
PS, why does java have to be so obtuse?
Sir, you are a life saviour. Thanks.
Hi,
Calendar.getInstance.getTime() is working fine except CST timezone, it is showing 1 hour less than the actual. Please help
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
Thank you Sir.
Thanks a lot.
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.
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
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
thanks
Hi
Very useful I came across many tutorials But you explained in both the ways which was helpful.
Thank you.
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.
how about get client’s date format…?
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?
Thank you :)
thanks, helped me
Very easy 2 understand…Highly useful for beginners! :)
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
“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();
Just show you two ways of getting current date and time, not comparison :)
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?
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.
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.
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