Get Current Date and Time in Android using Two Classes


Recently, I needed to get current date and time in android. I found two ways of doing it. They are by using the Calendar class and SimpleDateFormat class. So let’s see what method will be more simple and useful for you.

android-get-current-date-time

A simple outline of this post:

1.0 Using the Calendar Class
2.0 Using the SimpleDateFormat Class
3.0 My Thoughts
4.0 Helpful Comments

1.0 Using the Calendar Class

Here’s how to get android date and time using the Calendar Class.

// using Calendar class
Calendar ci = Calendar.getInstance();

String CiDateTime = "" + ci.get(Calendar.YEAR) + "-" + 
    (ci.get(Calendar.MONTH) + 1) + "-" +
    ci.get(Calendar.DAY_OF_MONTH) + " " +
    ci.get(Calendar.HOUR) + ":" +
    ci.get(Calendar.MINUTE) +  ":" +
    ci.get(Calendar.SECOND);

The output of the code above is:

2011-6-9 7:0:56

2.0 Using the SimpleDateFormat Class

And here’s how to get it using SimpleDateFormat Class.

// SimpleDateFormat Class
SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
String newtime =  sdfDateTime.format(new Date(System.currentTimeMillis()));

SimpleDateFormat code output will be:

2011-06-09 19:00:56

3.0 My Thoughts

As you can see, using the calendar class seemed like it require us to code more. Also, I think I found a bug. My device calendar settings are correct. It is month of June, so ci.get(Calendar.MONTH) must return “6” but it returns “5” so I had to add “1” to make the output correct.

In my case, I used the SimpleDateFormat class since I don’t really have to synchronize it. It is easy to format – you can just use its time pattern strings (in our example “yyyy-MM-dd HH:mm:ss”).

But please be aware that it was said in the docs that SimpleDateFormat is NOT thread safe when it comes to Synchronization.

You should read more about the Android’s Calendar Class and SimpleDateFormat Class. Thanks for sharing your thoughts about getting the current date and time in Android!

4.0 Helpful Comments

I love reading useful comments and so we will highlight them by including it in our post. Thanks Kovica and Charuta!

According to Kovica:

You didn’t find any bugs, because if you look closely at the docs for the java.util.Calendar class, you’ll see that JANUARY = 0 http://developer.android.com/reference/java/util/Calendar.html#JANUARY), so JUNE = 5 http://developer.android.com/reference/java/util/Calendar.html#JUNE)

Charuta Said:

You could use the Time class, it’s a faster implementation of Calendar class. http://developer.android.com/reference/android/text/format/Time.html

How about you, do you have any other solution or thought about getting current date and time in Android? Drop it in the comments section below! Thanks!


12 responses to “Get Current Date and Time in Android using Two Classes”

  1. Android’s documentation is sometimes lacking. In this case the official Java-API is more precise as it states: “The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0.”

    Still, I agree with you that this is contrary to one’s expectation :-)

    You should also have a look at Android’s DateFormat helper class which formats the Date according to the user’s language settings.

  2. Is this really worth of a post? For heaven’s sake getting the time and date is something a real developer would be able to do in their sleep.

Leave a Reply

Your email address will not be published. Required fields are marked *