Android get date and time

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.

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.

The output of the code above is:

2.0 Using the SimpleDateFormat Class

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

SimpleDateFormat code output will be:

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.

Читайте также:  Дурак для андроид apk

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)

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!

Источник

Programmers Sample Guide

All one can think and do in a short time is to think what one already knows and to do as one has always done!

Android get current Date and Time cheat sheet

Also you can use android.text.format.DateFormat

DateFormat.format(«MM/dd/yy h:mmaa», System.currentTimeMillis()

This class takes as inputs a format string and a representation of a date/time. The format string controls how the output is generated. Formatting characters may be repeated in order to get more detailed representations of that field. For instance, the format character ‘M’ is used to represent the month. Depending on how many times that character is repeated you get a different representation.

For the month of September:
M -> 9
MM -> 09
MMM -> Sep
MMMM -> September

The effects of the duplication vary depending on the nature of the field. See the notes on the individual field formatters for details. For purely numeric fields such as HOUR adding more copies of the designator will zero-pad the value to that number of characters.

Читайте также:  Андроид как выключить usb накопитель

For 7 minutes past the hour:
m -> 7
mm -> 07
mmm -> 007
mmmm -> 0007

Examples for April 6, 1970 at 3:23am:
«MM/dd/yy h:mmaa» -> «04/06/70 3:23am»
«MMM dd, yyyy h:mmaa» -> «Apr 6, 1970 3:23am»
«MMMM dd, yyyy h:mmaa» -> «April 6, 1970 3:23am»
«E, MMMM dd, yyyy h:mmaa» -> «Mon, April 6, 1970 3:23am&
«EEEE, MMMM dd, yyyy h:mmaa» -> «Monday, April 6, 1970 3:23am»
«‘Noteworthy day: ‘M/d/yy» -> «Noteworthy day: 4/6/70»

Источник

Оцените статью