Get days until date based on timezone on phone - android

I´ve been trying to figure this out for a while but can not wrap my head around it.
I´m working on an android app and i want to display left to a specific date, and i want the number of days based on what time zone i have set on my phone.
I have Joda Time in my app and the information i have is for example:
2013-05-05 9.00PM the time is in PST (GMT-8) timezone, i have no clue how to do this and i have searched both on google and SO but can not get a clear answer.
EDIT
I managed to solve my problem with code found here on SO
String dateString = airDate + " " + airTime.toUpperCase();
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd K:mma");
sourceFormat.setTimeZone(TimeZone.getTimeZone("GMT-8"));
Date parsed;
parsed = sourceFormat.parse(dateString);
TimeZone tz = TimeZone.getDefault();
SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd");
destFormat.setTimeZone(tz);
result = destFormat.format(parsed);
However I found out that the times i first got isn´t correct and I now get the time zone in the format GMT-5 +DST. And I dont´t know what to do with the +DST if setting the time to 20:00 and using GMT-5 in my TimeZone.getTimeZone the time returned is 22:00 which is "wrong" since I live in sweden. I would appreciate any help with this.

If you could settle for a string that looks like "in 2 days", then you should be able to use DateUtils.getRelativeTimeSpanString().

Related

How to get 2 date objects holding the current time, one in GMT and the other in local

I am trying to fill 2 date objects, one in Local time and the other in UTC.
I AM NOT TRYING TO PRINT THE DATE AS A STRING IN GMT/UTC, please do not suggest DateFormatting, and dont say its a duplicate until you read the full question.
Local, I have no problem:
Date dateLocal = new Date();
The problem is I cant get the utcDate to be UTC.
Using a Calendar like so:
TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
Calendar c = new GregorianCalendar();
c.setTime(new Date());
c.setTimeZone(TimeZone.getTimeZone(utcTimeZone.getID()));
Date utcDate = c.getTime();
When debugged or submitted to the webservice, utcDate shows in my local timezone, instead of UTC.
Using Joda:
DateTime utcDateTime = DateTime.now(DateTimeZone.UTC);
Date utcDate = utcDateTime.toDate();
Same issue, utcDate when debugged/submitted to webservice is showing in local time.
Here is how the object looks when debugged:
This is an issue because this causes the webservice (which i have no access to) to think this time is UTC, so when it does its work and conversions, the time is always off by 4 hours, since for me the UTC to Local conversion is GMT -4.
The ONLY way i have been able to get this to submit the date in UTC time is by adding:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
BUT this also changes the LocalTime object, even though this object was defined and set before the default TimeZone was changed.
So i get it, the Date() object uses the JVM locale, so any time a Date is created, its created in the default timezone, and apparently whenever the default timezone is changed, all of the Date objects (even if they are already created) change to the new default timezone... I know Date objects are just the millis between now and 1970 whatever, but the TimeZone is obviously being taken into account in the Webservice and this is messing up my results...how can i get the dates the way i want?

how to place a check that user can login only once in a day Android?

i have been thinking for hours but i find no way, no solution, can any body help me or give me a logical idea? or some lines of code? i am getting cuurent time and adding 24 hours extra when user trying second time to login but its not working for me.
here is my code for getting current time.
public void getTime() {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String strDate = sdf.format(c.getTime());
time = strDate.replace(":", "");
previous_time = Integer.parseInt(time);
saveToSharesPre(previous_time + 2400);
return;
}
Create shared preference for login_date. Whenever user logs in, check whether the current date is greater than the login_date from shared preference. If it is greater allow the user to login and update the login_date. Now if the user logs in for the second time in a day, the current date will not be greater than login_date, hence restrict him from logging in by showing some alert.
You can do this for hour also.Put hour instead of date in shared preference as say Monish Kamble.

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.

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

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