Aldo Ziflaj

About Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also a developer for Mozilla Albania team.

Java – Get current time and date

There are a couple of ways to get the current time and date in Java. You may get the number of milliseconds since January 1, 1970, 00:00:00 GMT using System.currentTimeMillis() and calculate the time by yourself (a problem given to beginners, but too much work if you are developing something), you may use a library not included in the JDK (like Joda-Time), or you may use java.util.Date and/or java.util.Calendar, these two Java SE classes. Note that here are some differences between them, which we will explore in this article.

In this example, I will show how to use each of them to get the date and time.

SimpleDateExample

Create a Java class called SimpleDateExample with the following source code:

package com.javacodegeeks.example;

import java.util.Date;

public class SimpleDateExample {

	public static void main(String[] args) {
		
		Date today = new Date();
		System.out.println("today: "+today.toString());

	}

}

This is the simplest usage of java.util.Date, where I “stringified” a Date instance. The output it gives is:

today: Sat Aug 09 11:30:50 PDT 2014

Of course, toString() is not the only method in java.util.Date. Although most of them are deprecated since JDK version 1.1, I will show their usage in the following example.

DateExample

Create a Java class called DateExample with the following source code:

package com.javacodegeeks.example;

import java.util.Date;

public class DateExample {

	public static void main(String[] args) {
		Date today = new Date();
		
		System.out.println("Today, the date is "+today.getDate());
		System.out.println("Today is the "+today.getDay()+" of the week");
		System.out.println("The time is "
					+today.getHours()+":"
					+today.getMinutes()+":"
					+today.getSeconds());
		System.out.println("This is the "+today.getMonth()+" month of the year");
		
		System.out.println(today.getTime()+" milliseconds have passed since Unix epoch");
		
	}

}

The output is this:

Today, the date is 9
Today is the 6 of the week
The time is 11:44:49
This is the 7 month of the year
1407609889718 milliseconds have passed since Unix epoch

Almost all of the methods I used in this example are Deprecated since JDK version 1.1 (released in February 19, 1997). The only one which is not deprecated is getTime() method.

Of course, java.util.Date comes with a set of setter methods for the getters on the example above, and (of course) the setters too are deprecated. As you can read in the online JavaDoc of java.util.Date, most of its methods are replaced by java.util.Calendar methods.

SimpleCalendarExample

Now I will show you how to write the above example using the not-deprecated java.util.Calendar methods. Create a class called SimpleCalendarExample with the following source code:

package com.javacodegeeks.example;

import java.util.Calendar;

public class SimpleCalendarExample {

	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		
		//getting all the information from the Calendar instance
		System.out.println("Today, the date is "+cal.get(Calendar.DAY_OF_MONTH));
		System.out.println("Today is the "+cal.get(Calendar.DAY_OF_WEEK)+" of the week");
		System.out.println("The time is "
								+cal.get(Calendar.HOUR_OF_DAY)+":"
								+cal.get(Calendar.MINUTE)+":"
								+cal.get(Calendar.SECOND));
		System.out.println("This is the "+cal.get(Calendar.MONTH)+" month of the year");
		
		System.out.println("Today is "+cal.getTime());
		
	}

}

The output is almost the same as in the previous example:

Today, the date is 9
Today is the 7 of the week
The time is 12:5:9
This is the 7 month of the year
Today is Sat Aug 09 12:05:09 PDT 2014

As you can see in the line 8 of the code, you can’t create an instance of the java.util.Calendar class by using a Constructor, since it is a locale-sensitive class. In order to get information about the date, time, and other fields, we use the get() method by passing one of the final variables defined in the java.util.Calendar class. Of course, java.util.Calendar has the non-deprecated version of the deprecated methods in java.util.Date.

Download Code

Download
You can download the full source code of this example here : TimeAndDateExample

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close