'How to use time in if else statement to change background of android app
I am using openweather api and in response i am getting date like 1648292136 after converting it to date it becomes Sat Mar 26 16:28:44 GMT+05:30 2022. To convert it i used:
Long updatedAt = response.getLong("dt");
Date ctime = new Date(updatedAt * 1000);
now I want if else statement like:
if(time between 6PM && 4AM)
{
app background = moon;
}
else
{
app background = sun;
}
Solution 1:[1]
String dateString = DateFormat.format("HH", new Date(updatedAt)).toString();
int dateHour = Integer.parseInt(dateString);
if(dateHour < 18 && dateHour > 4)
{
app background = moon;
}
else
{
app background = sun;
}
This will work completely fine for you
Solution 2:[2]
Thanks to @sambhav-khandelwal just needed to change some things like instead of "hh" it will be "HH" and if else statement will be something like this:
if((Integer.parseInt(dateString) >= 19))
{
//Night background
} else if (Integer.parseInt(dateString) >= 4)
{
//Day Background
} else
{
//night background
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Lakshay Bomotra |
