I have 3 different Date objects and I want to pass them to AlarmManager object to alert me when they occur.
May I need to use
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 2);
to add 2 minutes to the calender, but how to add a future date object to the calender ?
Do I need A Calender object, or I need a separate Calender object for each Date ?
Them for AralamManger do I need one object to 3 ?
I will advice you to use three different Calendar objects in which future dates will be set as below:
Calendar calendar1 = Calendar.getInstance();
Date future_date=new Date();
future_date.setHours(hour);
future_date.setMinutes(min);
future_date.set(year, month, day);
calendar1.setTime(future_date);
....OR.......
calendar1.set(year, month, day, hourOfDay, minute)
----------------
Look like u want to setup multiple alarm...below is link for answer
How can I setup multiple alarms in Android?
Related
I want to get which day of year is it.
a year have 365 days . I want to get which day base on the data , i have .
for example :
my date is : 2017/01/03 ---> base on this data I found that it means day 2.
You can use Calendar class to get your desired output:
Calendar cal = Calendar.getInstance(); // get calendar object instance
cal.setTime("set your time here"); // set your desired time here, it can be as Date object, if you've millis then use setTimeInMillis();
cal.get(Calendar.DAY_OF_YEAR); // get day of year like this
I am stuck at point where I need to ask for birthdate from user as input. I need to put restriction that user should not be able to add any date before 10 year).
I think you mean users can't add any date earlier than 10 year before right? Or your users are mostly kids aged 10 to 0?!
Since your limit date is based on current date, you have to set limit programmatically using setMinDate(long date) and setMaxDate(long date). As you can see those method works with date in millisecond so you have to get dare in millis first:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -10); //Goes 10 Year Back in time ^^
long upperLimit = calendar.getTimeInMillis(); //Get date in millisecond (epoch)
, and then set the limit using above method:
datePicker.setMaxDate(upperLimit);
You could do this:
DatePicker datePicker = (DatePicker) findViewById(R.id.event_date);
datePicker.setMinDate(dateTenYearsAgo);
More info: https://stackoverflow.com/a/18353944/4235666
try with this code in datePicker dialog:
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -10);
long tenYearBack = c.getTimeInMillis();
datePickerDialog.getDatePicker().setMinDate(tenYearBack);
I have an application where the user selects a date as well as a time (using DatePicker and TimePicker). Is there any way to combine all the values into one integer? Both the DatePicker and TimePicker return integers, if I add them up, will the value then be the selected date and time, or does it need to be done some other way?
The way I've been understanding date and time, is it gives the difference in milliseconds from a certain point. Based on that, I would guess that adding the amounts together would give the correct time (taking into account the different starting times of the various methods).
Thanks!
You can use a Calendar, set the components then get the date.
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 37);
cal.set(Calendar.DAY_OF_MONTH, 13);
cal.set(Calendar.MONTH, Calendar.JANUARY);
Date d = cal.getTime();
long time = cal.getTimeInMillis();
I'm using Java's calendar to set an alarm at a specific date and time. I know how to make this work when the user selects a specific date and time. For example, if the user wants to set an alarm on July 17th, 2013 at 10:45AM, I'm using the following code:
//Get the calendar instance.
Calendar calendar = Calendar.getInstance();
//Set the time for the notification to occur.
calendar.set(Calendar.YEAR, 2013);
calendar.set(Calendar.MONTH, 6);
calendar.set(Calendar.DAY_OF_MONTH, 17);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
All of the above code works really well when I want to set an alarm at a specific date and time. My question is, how can I set a calendar instance where the user user wants an alarm to go off 20 minutes from the current date and time? So if the current time is 6:50PM, I need the alarm to go off at 7:10PM. How can I set this programmatically?
I tried to get the current date and time via the Java.util.calendar's built-in methods and tried adding 20 minutes to the Calendar.MINUTE variable. However, I don't think this will do the trick if the current time is less than 20mins away from midnight (the date will change), or 20mins away from another hour (the hour will change). How can I get around this problem? Thanks for all your help!
You want to look at calendar.add , it will increment the next field if you get overflow.
http://developer.android.com/reference/java/util/Calendar.html
You can also try this
Calendar c = Calendar.getInstance();
c.setTime(new Date()); //
c.add(Calendar.YEAR, 5); // Add 5 years to current year
c.add(Calendar.DATE, 5); // Add 5 days to current date
c.add(Calendar.MONTH, 5); // Add 5 months to current month
System.out.println(c.getTime());
You can try the calendar.set methods specified in this links.
How to set the calendar in android for particular hour
This worked for me , when I wanted to run the service on the specified time.
I have a two dates for example 17/3/2012 and 18/3/2012 and I want to set these dates in alarm manager . how to use calendar instance to add them?
You can use the Calendar class and its set() method. It takes two parameters: a Calendar field, such as YEAR, MONTH, HOUR_OF_DAY, and the value of the field. You must initialize the Calendar object, setting all the fields as you need, and then call getTimeInMillis() to get the time in milliseconds. Here's a link to the Calendar reference: Calendar. Hope this helps.