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.
Related
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.
i have a monthdisplayhelper to display the month, i want to select days between two date. i.e.,if i selected 2nd July and 9th July, all the dates(3rd to 8th) in between these dates must be selected or highlighted.
You can use the Calendar class. Initialise a new Calendar object for your initial date, then use [calendar instance].add(Calendar.DAY_OF_YEAR, 1) to increment the date by 1 day, it'll automatically roll to the next month if required.
Calendar reference: http://developer.android.com/reference/java/util/Calendar.html
If you know how many days you will need then you could have a loop basically add days and store in a list
in my application I got the date and time via DatePicker and TimePicker and set reminder on Perticular user selected Date and Time, But I already done Date and Time Picker but don't know how to set reminder on that, Kindly help me.Thanks
Set an Alarm using AlarmManager, or create a Calendar event using the Calendar Provider API.
Android provides a permission called "SET_TIME_ZONE" with OS permission level "dangerous". Does anyone know that given an application with this permission, how can the app set the time zone ?
Thanks.
If your objective is to change the system's default time zone, then use setTimeZone() of AlarmManager.
You can set the TimeZone in multiple ways:
You can use TimeZone.setDefault() which will change the TimeZone for the current process only. But as noted in the docs, this is not garanteed to last for the whole application lifecycle.
You can use setTimeZone() of AlarmManager to change the TimeZone of the whole device. But you need the "SET_TIME_ZONE"-permission for that.
If you think 1. is to dangerous and you don't have the permission for 2. your best approach is to get every Date from Calendar and set the TimeZone on your Calendar-instance via setTimeZone().
For Setting of the Time Zone Programetically you need to use the Date Class.
See its Reference Documents here.
You need to use the setTimeZone() method of SimpleDateFormat Class.
Following is sample code for settings Time Zone of according to America
// First Create Object of Calendar Class
Calendar calendar = Calendar.getInstance();
// Now Set the Date using DateFormat Class
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
// Finally Set the time zone using SimpleDateFormat Class's setTimeZone() Method
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
How can I construct a Calendar object using getInstance(TimeZone) to use the device's TimeZone?
According to this issue, just using:
Calendar calendar = Calendar.getInstance();
will give you an instance in the user's default time zone (as per their settings). You can determine the time zone from the calendar with:
TimeZone zone = calendar.getTimeZone();
Other posts have suggested that using TimeZone.getDefault() does not give this user-default time zone - I don't know about that personally, but it's another option to look into.