In my application I want to change background with Day/Night time.
My mean is I want check time if time is night set night background from drawable and when time is Day set day background from drawable.
Sorry for my bad question, I am an amateur and really need your help.
How can I do it in android?
You can use a code something like this, by specifying the hours you consider in morning and the hours you consider as night.
I considered the time from 06:00 - 18:00 as morning and 18:00 - 05:00 as night. Change it accordingly.
Calendar calendar = Calendar.getInstance();
int time = c.get(Calendar.HOUR_OF_DAY);
if(time >= 6 && time < 18){
image.setImageResource(R.drawable.morning);
}else if (time >= 18 && time < 6){
image.setImageResource(R.drawable.night);
}
EDIT
As suggested by #AxelH, it would be better to just specify either one of the time (morning or night only). The code would be something like:
Calendar calendar = Calendar.getInstance();
int time = c.get(Calendar.HOUR_OF_DAY);
if(time >= 6 && time < 18){
image.setImageResource(R.drawable.morning);
}else{
image.setImageResource(R.drawable.night);
}
Please try this:
Calendar time = Calendar.getInstance();
int gettime = time.get(Calendar.AM_PM);
if(Calendar.AM == gettime)
Log.d("AM"+now.get(Calendar.HOUR));
Related
I've created an Android grading app to use within my classes. All the database connections and grading logic has been laid out and functions precisely as it should.
However, I am having a problem incorporating the Java Calendar class. I need to be able to limit each class to submit their answers "only" during their class time. As you can see, I will need to incorporate a series of "if statements" in order to do this.
The problem is that I don't know how to get this time that I need to verify that my student's answer submissions are only handled during their class time.
This is what I've tried:
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
As you can see, I am only able to get hours and minutes separate from each other. How do I get the real time? Just Hours and minutes not seconds.
In my code my set up should look like this:
int period = will be selected from a spinner object
if(period == 1 && (time>= 7:20 AM && time<= 9:00AM)) Then, go ahead and
submit your answers to online database.
if(period == 8 && (time>= 12:50PM && time<= 2:20PM)) Then, go ahead and
submit your answers to online database.
I don't know how to format this "time" object with the Calendar class.
Any help would be appreciated.
You can set hours and minutes to the calendar:
Calendar start = Calendar.getInstance();
start.set(Calendar.HOUR, 7);
start.set(Calendar.MINUTE, 20);
The same for the endDate:
Calendar end = Calendar.getInstance();
end.set(Calendar.HOUR, 9);
end.set(Calendar.MINUTE, 0);
To check that the current datetime is in the interval:
Calendar cal = Calendar.getInstance();//now
boolean isInInterval = cal.getTime().after(start.getTime()) && cal.getTime().before(end.getTime());
I am trying to check if Current Hour falls in between 7AM and 10AM
I am doing it this way
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int hour = cal.get(Calendar.HOUR_OF_DAY);
if(hour >= 7 && hour <= 10)
{
}
Please tell me if this is a valid approach to follow
Yes that is a valid approach. You could also use the new Time API (needs Java 8)
import java.time.LocalTime;
...
int hour = LocalTime.now().getHour();
It's a bit shorter to get the hour. There is also an overloaded now(ZoneId zoneId) method.
Or make it a one liner with a temporal query (also needs Java 8):
Boolean isBetween7and10AM = LocalTime.now().query(date -> date.get(ChronoField.CLOCK_HOUR_OF_DAY) >=7 &&
date.get(ChronoField.CLOCK_HOUR_OF_DAY) < 10);
Yup. This looks fine. Remember that this Calendar field is dependent on the timezone.
BTW: Put the starting curly brace at the end of the previous line, since this is the common code formatting style.
:-)
I would like to know if the current time has passed 12 pm already. Can I do it in an if statement like:
if(timenow is already pass 12 pm){
**do code here**
}
Is there a java code or a method in joda time that can do that? All answers are appreciated. Thanks. :)
Calendar cal = Calendar.getInstance();
if (cal.get(Calendar.HOUR_OF_DAY) >= 12) {
...
}
JAVA
int hour = LocalDateTime.now().getHour();
JODATIME
DateTime dt = new DateTime(); // current time
int hour = dt.getHourOfDay(); // gets hour of day
and put the logic here
if(hour >= 12){
do code here
}
I'm trying to create a method which is checking if "today" is between Monday and Friday. For this I get with this line 'int day = Calendar.DAY_OF_WEEK;' the actual day. After that I fill a ArrayList with the days (Monday, Tuesday, Wendsday, Thursday and Friday). Now when I check if the actual day is in my ArrayList, i set boolean DAY = true else i set boolean DAY = false. I tryed the Method today and yesterday, but it allways sets the boolean to false.
What do I need to change that my code works? You'll find the code down here.
Code
int day = Calendar.DAY_OF_WEEK;
ArrayList<Integer> daylist = new ArrayList<Integer>();
daylist.add(Calendar.MONDAY);
daylist.add(Calendar.TUESDAY);
daylist.add(Calendar.WEDNESDAY);
daylist.add(Calendar.THURSDAY);
daylist.add(Calendar.FRIDAY);
if (daylist.contains(day)){
DAY = true;
}else{
DAY = false;
}
Wow, that's like trying to kill a mosquito with a thermo-nuclear warhead :-)
Java guarantees (in 1.5) (unchanged up to 1.8 at least) that the values of SUNDAY through SATURDAY are contiguous (1 through 7) so it's a simple matter of checking a range.
However, DAY_OF_WEEK is not the day of the week, it's a field number (with the value 7) to be passed to the getter to retrieve the day of the week. The only time Calendar.DAY_OF_WEEK itself will match an actual day will be on Saturdays.
You can use code such as:
Calendar myDate = Calendar.getInstance(); // set this up however you need it.
int dow = myDate.get (Calendar.DAY_OF_WEEK);
boolean isWeekday = ((dow >= Calendar.MONDAY) && (dow <= Calendar.FRIDAY));
Following this, isWeekday will be true if and only if the day from myDate was Monday through Friday inclusive.
int day = Calendar.DAY_OF_WEEK; should instead be
Calendar cal; // The calendar object
....your other code for getting the date goes here....
int day = cal.get(Calendar.DAY_OF_WEEK);
Your current code just gets the value of the constant Calendar.DAY_OF_WEEK.
This should do the trick for you i assume.
int day = cal.get(Calendar.DAY_OF_WEEK);
if (day >= Calendar.MONDAY && day <= Calendar.FRIDAY){
DAY = true;
}else{
DAY = false;
}
int day = Calendar.DAY_OF_WEEK;
The logic is broken right here. DAY_OF_WEEK is a constant identifying which type of data we need to retrieve from a Calendar instance.
The simplest solution to your problem (since Calendar.FRIDAY > ... > Calendar.MONDAY) is
Calendar now = Calendar.getInstance();
int day = now.get(Calendar.DAY_OF_WEEK);
if (day >= Calendar.MONDAY && day <= Calendar.FRIDAY)
// do something
First Calendar.DAY_OF_WEEK is an integer field will always gives you 7. You need to create an instance of a Calendar like Calendar cal = Calendar.getInstance(); By default it gives you the current date in current timezone.
Then you can call cal.get(Calendar.DAY_OF_WEEK); which will give you any day between Sunday and Sat'day
Now you can check something like this
if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
System.out.println("Weekend");
} else {
System.out.println("Weekday");
}
You can apply this logic to your problem!!
I have an application that passes in java.util.Date. I want to check whether this date is within a specified time of day (e.g. between 10:30 & 11:30), I don't care about the date, just the time of day.
Can anyone show me a simple way to do this?
Thanks
This is what the Calendar class is for. Assuming date is your Date object:
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int hour = cal.get(Calendar.HOUR_OF_DAY);
int minutes = cal.get(Calendar.MINUTE);
if (hour == 10 && minutes >= 30 || hour == 11 && minutes <= 30) {
...
}