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.
Related
I want to convert a timestamp like 62207486144 to days(like 1 year 6 months 2 days 3 hours 33 minutes) in my Android App. How can I do that? I am able to get days and hours but not years or months with the following code-
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(62207486144);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(calendar.getTime());
long timestamp = 62207486144;
long days = TimeUnit.MILLISECONDS.toDays(timestamp );
timestamp -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(timestamp );
Years- divide days by 365 (or 365.25 if you want to account for leap years). Months- well, months aren't exact because months aren't the same length, but dividing by 30 is going to be about right.
Your code above is a bit odd though. The first 4 lines are doing something totally different than the last 4. The first 4 would get you data about a specific time in a timestamp- you'd use that if you wanted to figure out for a timestamp what day/month/year it was. The last 4 treat it as a duration. You'd use that for figuring out how long something took. My suggestion above works for durations. If you want to know when a particular timestamp was instead, you'd just use the calendar object to tell you that.
check this out, as an easy way to convert to localDateTime.
From there, it should be way easier.
long millis = 62207486144L;
LocalDateTime date = Instant.ofEpochMilli(millis).atZone(ZoneId.systemDefault()).toLocalDateTime();
date.getDayOfMonth(); //Day
date.getMonthValue(); //Month
date.getYear(); //Year
More information here: https://howtoprogram.xyz/2017/02/11/convert-milliseconds-localdatetime-java/
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.
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 know this is very simple question but I am not able to do it.
I have a code that gets current time but this time is not accurate.
booking.CreateDateTime = DateTime.Now.ToUniversalTime();
When I am booking at 12:00 then in database stores 1:00 that means 1 hour difference.
How can I get accurate time?
Use System.currentTimeMillis() to get the current GMT time in mili seconds since epoch.
Then you can use this value to create a new Date or Calendar object and localize it wherever the user is.
I'm not familiar with what you have there, but ToUniversalTime suggests to me that this is adjusting your time to some fixed time zone (probably GMT)
Use a Date to get the time right now, and then a Calendar to do any time zone changes on it that you want.
Example, assuming CreateDateTime is actually a string of what you said it was:
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
booking.CreateDateTime = calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));