Is setTimeZone method is device independent? - android

My App is heavily dependent on time. If I set the time zone using below code and user device time/date is not set correctly then will it work correctly and return correct time?
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));

This will not help if the device time is off. By setting the timezone, you are just telling the formatter to display (what the device thinks is) the current time with an offset of +7/+8 hours.

Related

How to get current UTC or GMT date time in millis in android instead of getting device's current date time [duplicate]

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")).getTimeInM‌​illis()
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.

source Code for fixed device date and time

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

Android - Changing Phone's Timezone

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.

SensorEvent.timestamp date conversion issue

I'm trying to convert event timestamp in a date, my code:
Calendar c = Calendar.getInstance();
c.setTimeInMillis(event.timestamp/1000000);//time in ms (timestamp is in ns)
System.out.println((new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(c.getTime()));
How come I get 1970-01-02?
I assume that you are using SensorEvent.timestamp. The documentation fails to mention that this is the awake time in nanoseconds since the last boot (comparable to SystemClock.uptimeMillis()), not time since Unix epoch. In short, it appears your device has been awake for less than two days.
Also, Calendar.getTime() returns a Date object and there is a Date constructor that takes milliseconds so you can shorten your code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date(event.timestamp / 1000000)));

Get time of different Time zones on selection of time from time picker

I have an issue of converting selected hours and minutes to different time zones of countries.
Supposing if i select 10 am in India then i want to know at 10 am in india what will be the time in USA/New york and Tokyo.and Vice versa.
Any help is appreciable...
Thank you
please find the sollution below :
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mma");
TimeZone timezone = TimeZone.getDefault();
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Date d = new Date();
sdf.setTimeZone(timezone);
String strtime = sdf.format(d);
Log.e("str time gmt ",strtime);
sdf.setTimeZone(utcTimeZone);
strtime = sdf.format(d);
Log.e("str time utc ",strtime);
i think this will solve your problem
You can probably use Joda Time - Java date and time API. You can get the DateTimeZone depending on the Canonical ID defined in the Joda Time,
DateTimeZone zone = DateTimeZone.forID("Asia/Kolkata");
Joda Time has a complete list of Canonical ID from where you can get TimeZone depending on the Canonical ID.
So, if you want to get the local time in New York at this very moment, you would do the following
// get current moment in default time zone
DateTime dt = new DateTime();
// translate to New York local time
DateTime dtNewYork = dt.withZone(DateTimeZone.forID("America/New_York"));
For getting more idea you can refer Changing TimeZone
Try using Joda-Time library
check the org.joda.time.DateTimeZone class
Here is the API documentation for the same.
you can also get it using , Here no external API is needed
DateFormat format = new SimpleDateFormat("MMMMM d, yyyy, h:mm a");
TimeZone utc = TimeZone.getTimeZone("America/New_York");
System.out.println(utc.getID());
GregorianCalendar gc = new GregorianCalendar(utc);
Date now = gc.getTime();
System.out.println(format.format(now));
you can see more time zone on this Link
Output
America/New_York
December 29, 2012, 11:04 AM
If you don't know city name then you can also use it by Zone name as follow
DateFormat format = new SimpleDateFormat("MMMMM d, yyyy, h:mm a");
TimeZone cst = TimeZone.getTimeZone("US/Eastern");
System.out.println(cst.getID());
GregorianCalendar gc = new GregorianCalendar(cst);
Date now = gc.getTime();
format.setTimeZone(cst);
System.out.println(format.format(now))
Output
US/Eastern
December 29, 2012, 12:38 AM
Not really sure about the solution I'm going to provide but I think you can try it. GMT (Greenwich Mean Time) is a standard. I think you can keep it as a base and calculate the desired time. GMT standard is easily available too.
For example: While installing an OS like Windows XP or Windows 7, we select the time from a drop down menu. My point is, keeping this as the base, you can find the difference between the time zones in NY-US and Tokyo-Japan or vice versa as you desire it.
Hope this helps.

Categories

Resources