Getting current time android

Getting current time android

TrueTime for Android

Make sure to check out our counterpart too: TrueTime, an NTP library for Swift.

NTP client for Android. Calculate the date and time «now» impervious to manual changes to device clock time.

In certain applications it becomes important to get the real or «true» date and time. On most devices, if the clock has been changed manually, then a new Date() instance gives you a time impacted by local settings.

Users may do this for a variety of reasons, like being in different timezones, trying to be punctual by setting their clocks 5 – 10 minutes early, etc. Your application or service may want a date that is unaffected by these changes and reliable as a source of truth. TrueTime gives you that.

You can read more about the use case in our blog post.

In a recent conference talk, we explained how the full NTP implementation works with Rx. Check the video and slides out for implementation details.

Also, once we have this information it’s valid until the next time you boot your device. This means if you enable the disk caching feature, after a single successful NTP request you can use the information on disk directly without ever making another network request. This applies even across application kills which can happen frequently if your users have a memory starved device.

Wiki has a lot of useful information

Take a look at the wiki sidebar which should have a lot of useful information.

Is this library still maintained?

Читайте также:  Клавиатура андроид для толстых пальцев

The short answer is yes. We’re planning a complete rewrite of TrueTime (Version 4). See this branch. We juggle our personal time between doing this rewrite and using TrueTime in its current version. TrueTime is still used in its current form at Instacart by many many users.

As is the case with most open source libraries, maintenance is hard. We’ve not done a great job at answering the various issues, but we want to do better. We can only ask that you trust that the rewrite is coming and it should make things better. We don’t want to spend too much time making changes to the library in its existing state, because the rewrite is the better future for TrueTime.

Источник

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.

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!

Источник

Getting current time android

TrueTime for Android

Make sure to check out our counterpart too: TrueTime, an NTP library for Swift.

NTP client for Android. Calculate the date and time «now» impervious to manual changes to device clock time.

In certain applications it becomes important to get the real or «true» date and time. On most devices, if the clock has been changed manually, then a new Date() instance gives you a time impacted by local settings.

Users may do this for a variety of reasons, like being in different timezones, trying to be punctual by setting their clocks 5 – 10 minutes early, etc. Your application or service may want a date that is unaffected by these changes and reliable as a source of truth. TrueTime gives you that.

Читайте также:  Андроид аккаунт google что это

You can read more about the use case in our blog post.

In a recent conference talk, we explained how the full NTP implementation works with Rx. Check the video and slides out for implementation details.

Also, once we have this information it’s valid until the next time you boot your device. This means if you enable the disk caching feature, after a single successful NTP request you can use the information on disk directly without ever making another network request. This applies even across application kills which can happen frequently if your users have a memory starved device.

Wiki has a lot of useful information

Take a look at the wiki sidebar which should have a lot of useful information.

Is this library still maintained?

The short answer is yes. We’re planning a complete rewrite of TrueTime (Version 4). See this branch. We juggle our personal time between doing this rewrite and using TrueTime in its current version. TrueTime is still used in its current form at Instacart by many many users.

As is the case with most open source libraries, maintenance is hard. We’ve not done a great job at answering the various issues, but we want to do better. We can only ask that you trust that the rewrite is coming and it should make things better. We don’t want to spend too much time making changes to the library in its existing state, because the rewrite is the better future for TrueTime.

Источник

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