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.
Related
How to get correct Date and time for my application,when wrong android device date and time.need to source .
please help me.
thank you all!
You may need to use a custom NITZ or NTP Library
http://groups.google.com/group/android-developers/browse_thread/thread/d5ce3bcfe17e272b?pli=1
So just a heads up that Android uses NITZ events provided by a carrier to properly set the system date and time. Android also falls-back to network NTP automatically when no cellular network is available.
http://en.wikipedia.org/wiki/NITZ
The time provided by currentTimeMillis() will typically be the best available time, and it's what all of the services on the device use, like Calendar and Alarm Clock.
However
However the NTP API isn't somewhere that Java code can access, which means we're back to using an existing Java NTP/SNTP client library if we want an accurate time regardless of whether we are on a network that is NITZ capable.
Java NTP library
You can find a naive implementation of a Java NTP Library from
support.ntp.org
first you try to get the current time zone and then you get date and time of current time zone
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
Log.d("Time zone","="+tz.getDisplayName());
public String getTime(String timezone) {
Calendar c = Calendar.getInstance();
c.setTimeZone(TimeZone.getTimeZone(timezone));
Date date = c.getTime();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String strDate = df.format(date);
return c.getTime().toString();
}
Is is possible to set the phone's timezone programmatically in android? I got this code
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println(sdf.format(calendar.getTime()));
but it does not change the phone's timezone. It only displays the timezone of America/Los Angeles.
The code you are showing simply defines a Calendar instance with a specific format to be used in the app.
It is not possible to change the phone's timezone programmatically.
You could redirect the user to the appropriate settings:
startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
Well, this only changes formatting, so you cannot expect it to change phone's time zone. And no, there are no APIs for changing time or timezone, this is reserved for system apps.
I have an Android app that is used by people in the UK and Ireland only, and there are no plans for this app to be used overseas.
In the app I store dates for various things, although I never need the full timestamp including the time of day, I only need the date.
So that I can compare dates easily, I've been creating calendar objects and clearing the values of the time, and using the milliseconds of that to store in the database.
public Calendar clearCalTime(long l) {
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTimeInMillis(l);
cal.clear(Calendar.HOUR);
cal.clear(Calendar.HOUR_OF_DAY);
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
return cal;
}
The issue has been that if users change the timezone, for some reason the dates start messing up, e.g by saving things to the wrong day. The timezones could be anything, I have no control over what the users set.
I've tried setting the timezone to UTC but this doesn't work either. Is there any way to just disregard the timezones?
Nope. You are going to have to set TimeZone going in and out of your storage.
You'll need to do this:
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
in your code to make it consistent.
See this SO: Java.util.Calendar - milliseconds since Jan 1, 1970
Alternatively use Joda Time when dealing with complicated Date/Time math.
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.
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"));