Android: Combining the values of Date and Time into one value - android

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();

Related

How to allow the user to choose date only before 10 years from today from datepicker in android?

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);

Getting Current Time for Booking

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));

How to set Java.util.calendar to a specific time period in the future

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.

Android: getting the time

i am currently using the following line to achieve the time: System.currentTimeInMilis.
I have noticed it doesn't consider time zones,or does it not match the android phone it self by the time, while on the emulator it does match.s
so is there another type of way to get the android clock it self? so when the user adjusts he's phones built in clock, it affects it too?
float getTime()
{
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
cal.setTimeZone(TimeZone.getDefault());
return cal.getTimeInMillis();
}
Read the documentation of currentTimeMillis. It has a time zone, which happens to be UTC (which is the default for Unix time stamps).
If you want to convert it to a different time zone you can make use of the Java Calendar and TimeZone classes:
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(System.currentTimeMillis());
cal.setTimeZone(TimeZone.getDefault());
Alternatively you can just create a new GregorianCalendar instance. By default its TimeZone will match the local one (as set on the device) and the time will be set to "now".
There are also other ways for retrieving the current time according the current time zone and locale as string. Take a look at DateUtils.
EDIT Explaining the usage of Calendar
Read the documentation for Calendar.getTimeMillis(). That method returns the Unix time stamp again which happens to have the time zone UTC.
You have to use the Calendar.get() method instead for getting the correct values. See following example for getting the current hour in the correct time zone via your calendar object:
int hour = cal.get(Calendar.HOUR_OF_HAY);
Read the documentation of Calendar. There are plenty of fields like HOUR_OF_DAY which help you getting values like the year, month, minute, seconds etc.

How to add 3 Dates to AlarmManager in android

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?

Categories

Resources