Date

Java Get current date and time

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.

1. SimpleDateExample

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

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:

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

2. DateExample

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

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:

1
2
3
4
5
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.

3. 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:

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:

1
2
3
4
5
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.

4. Download the source Code

Download
You can download the full source code of this example here: Java Get current date and time

Last updated on May 25th, 2020

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 one of the co-founders of Things Lab.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button