converting time from utc to device timezone and add 9 minute - android

I am having some doubts regarding the conversion of time from utc to device time zone.I am getting date from Server and that is in UTC. I convert that corresponding Time to my device Time zone. But my device time is Showing 10 minutes backwards. My requirement is that I need to add 9 minutes to my Device Time Zone. So if anyone knows the answer..please do help
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date timestamp = new Date();
timestamp = df.parse(s);
df.setTimeZone(TimeZone.getDefault());
date.setText(tk.nextToken());

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.

Getting Arizona time with getTime()

date= Calendar.getInstance();
Date currentDate = date.getTime();
String sDate = currentDate.toString();
This returns time EST. I need to change it to Arizona time which is tricky because Arizona does not have daylight savings time. Is there a short cut to making the changes or do I need to query a calendar to subtract two hours when Arizona is on MST and three hours when PST.
This returns time EST.
Well, Date.toString() will, if you're in EST at the moment. It's not part of the data stored within the Date - that's just an instant in time, with no idea what time zone or calendar system it might have started off in.
Your first two lines would be more simply written as:
Date currentDate = new Date();
You should use a DateFormat to convert the Date into a String. You can specify the time zone you want to use there. Do not start performing any arithmetic on the date yourself to add/remove offsets - that's a sign that you're heading in the wrong direction.

how to display date format according to device time zone

I want to display date according to time zone... if its america than month date year and if its europe than date month year..it check the current date setting from device and convert current date and time according to time zone.
my code is
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
String currentDateandTime = today.monthDay+"/"+today.month+"/"+today.year+" "+today.hour+":"+today.minute;
can anybody suggest me some modification to have date organization according to time zone
Thank you in advance
You can use the DateFormat class.
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG,Locale.FRANCE);
To get the users current Locale you can do
Locale.getDefault();
Be warned though it might not be what the user wants.

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