I am trying to post a notification every day at 10 am.
The notification is sent daily already, but not at 10 am, so I need to calculate the time to wait until its 10 am again for my AlarmManager.
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
long target = dateFormat.parse("10:00").getTime();
I tried this, but the timestamp I get is 50 years or so ago... (I think it is the time of the first time it was 10 am after the timestamp started counting)
So how do I calculate the milliseconds to wait until it is 10 am again?
Use Calendar instead of SimpleDateFormat for time-manipulation purposes.
In order to find 10AM on the next day:
final Calendar c = Calendar.getInstance()
c.set(Calendar.HOUR_OF_DAY, 10) // For 10AM
c.add(Calendar.DATE, 1) // Add 1 to date == tomorrow
final Date d = c.getTime()
// Do what you want with the date
You should take one Calendar object and set Hour of the day 10 AM than should get time from that calendar object like calender.getTime() it will return long and it will be set as setRepeating(calender.getTime()) while setting alarm manager.
Related
This question already has answers here:
How can I get the current date and time in UTC or GMT in Java?
(33 answers)
Closed 5 years ago.
I am trying to get current UTC date time in millis.
But every code I used for this returns me the device's current date time.
When I chenge my device's date, time it shows me chenged one.
So, I want to get GMT/UTC date time so that it will show me correct date even if user changes the date, time of his/her device.
Codes I tried:
Calendar calendar = Calendar.getInstance();
long now = calendar.getTimeInMillis();
and
DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("UTC"));
String gmtTime = df.format(new Date());
Date gmtDate = df.parse(gmtTime);
Actually I want to set an alarm at November 15 2017, 5 PM using AlarmManager, receive that event hide some activities in my app which I don't want to show after this date, time.
How can I acheive this?
Thanks in advance!
Use this ....
Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis()
for eg.
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long timeInMili = calendar .getTimeInMillis();
or
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
long timeInMili = calendar .getTimeInMillis();
Try TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
long timestampMilliseconds =System.currentTimeMillis();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String stringDate = simpleDateFormat.format(new Date(timestampMilliseconds));
System.out.println(stringDate);
if do you want to get as utc just change the time zsone
There is no way to get the correct time from device independently, If you are using Google Location Provider then getTime() will return derived time from GPS signal, else use server time.
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 am making a simple alarm clock application that mimics the default alarm app that comes with Android Lollipop.
The set*( ) methods of an AlarmManager require the date on which the alarm should be fired as a Unix epoch.
The UI is rather simple with a TimePicker.
So, given the current time and the time the user has selected from TimePicker, how do I figure out the time in milliseconds when the alarm should be fired?
Update:
There are two cases I run into:
Selecting the time that is after the current time:
Assume it is 11am and the user selects the time from the time picker as 03pm. In this case, I know that the alarm should be scheduled for the same day.
Selecting the time that is before the current time:
Assume it is 11am and the user selects the time from the time picker as 10am. In this case, I know that the alarm should be scheduled for the next day's 10am.
Ok here you go:
// Get the current time
final Date currentTime = new Date();
// Set the hours and minutes from the time picker against todays date
final Calendar selectedTime = Calendar.getInstance();
selectedTime.set(Calendar.HOUR_OF_DAY, hourFromTimePicker);
selectedTime.set(Calendar.MINUTE, minuteFromTimePicker);
// If the current date is greater than the hour and minute from time picker add one day
if (currentTime.getTime() > selectedTime.getTime().getTime()) {
selectedTime.add(Calendar.DAY_OF_YEAR, 1);
}
// Schedule the alarm
//AlarmManager.set(selectedTime.getTime().getTime());
if you store data in java Date object:
long getTime( )
Returns the number of milliseconds that have elapsed since January 1, 1970.just subtract.
Another way if you look only at time within day:
int ms = 1000*SECONDS + 1000*60*MINUTES + 1000*60*60*HOURS
I would use a android.text.format.Time class
call the setters on the Time class to set the Hour, Minute, Second, etc. The Hours are in 24H time, so if the current hour > selected hour then you know to increment days by 1
Finally, call Time#toMillis(boolean ignoreDst) to get the system time in millis, and pass that to AlarmManager
EDIT: GregorianCalendar should be used instead.
I am using timepicker. I can get date(calendar) in milliseconds but cant figure out with timepicker
with date i used:
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("US/Central"));
cal.setTimeInMillis(calendarView.getDate());
Date date = new Date(calendarView.getDate() / 1000L);
long timestamp = date.getTime()
//and this works
But i dont know with timePicker becouse i get two int's: hour, min.
Im just out of plays and dont know much more to try.. Could use the help
SOLUTION:
Dummy me just need to say the problem outloud.. Hopfully it will help someone else..
long hour = timePicker.getCurrentHour();
long min = timePicker.getCurrentMinute();
hour = TimeUnit.HOURS.toMillis(hour);
min = TimeUnit.MINUTES.toMillis(min);
TimePicker is design for 24hours of day, thus you cant get date within it just hours and minutes within the day.
as the documentation is saying:
A view for selecting the time of day, in either 24 hour or AM/PM mode.
I would recommend using DatePicker to enable you to get the date.
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.