Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current hour in android?

Tags:

android

Time class is no longer possible to use. I want to ask you, how to detect in app 3-4am? I need that to set up for example night mode in my app.

Can you give me some example how to do it?

like image 763
MakingMoneyWithAndroid Avatar asked Oct 21 '16 02:10

MakingMoneyWithAndroid


People also ask

How do I display the current time on my Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have given text view, it going to print the current date on the window manager.

Which view is used to display the date and time in Android application?

text. DateFormat. getDateTimeInstance(). format(new Date()); // textView is the TextView view that should display it textView.

How does Android store date and time?

Saving DateTime in Android String – you can simply save your datetime as String (Text in SQLite) if all you want to do with the date going forward is display it. Also you can use DateFormat to parse the saved String to date value and work with it later. Product temp = new Product(); temp.


1 Answers

Instead of using Time (because Time class was deprecated in API level 22.) you can use Calendar for getting current hour

val rightNow = Calendar.getInstance() val currentHourIn24Format: Int =rightNow.get(Calendar.HOUR_OF_DAY) // return the hour in 24 hrs format (ranging from 0-23) val currentHourIn12Format: Int = rightNow.get(Calendar.HOUR) // return the hour in 12 hrs format (ranging from 0-11) 
like image 95
Linh Avatar answered Sep 29 '22 02:09

Linh