Convert Time received from server(which is UTC timing ) to Local timing - android

I'm receiving time from server which follows UTC time zone & follows 12 HR Format like this(7/30/2013 6:44:22 AM)
Can anyone pls. tell me how to get this particular timing converted to Local time & display it as (12:20 )(which must be in 24Hr format)
Here is the code I tried :
object._chatCreatedDateTime=obj.getString("CreatedDateTime");
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
date = format.format(Date.parse(object._chatCreatedDateTime));
object._chatCreatedDateTime=date;
Here (object._chatCreatedDateTime) is the time received from server & I converted it to particular format & push it back to local database which is not yielding proper result.

To get the time in 24 hour patern use
"H:mm"
as time pattern string.
H represents hour in day(0-23)
So modify ur code as below,
object._chatCreatedDateTime=obj.getString("CreatedDateTime");
SimpleDateFormat format = new SimpleDateFormat("H:mm");
format.setTimeZone(TimeZone.getTimeZone("UTC"));date = format.format(Date.parse(object._chatCreatedDateTime));
object._chatCreatedDateTime=date;

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?

Android Joda Time - How to Load UTC from File

I am using Joda Time. I want to compare 2 dates/times A and B to determine if B has exceeded A.
Problem - When I logcat A it's not in UTC. When I logcat B it's in UTC. Therefore the comparison is not real.
A - A UTC time loaded from a file
B -The "System Time" in UTC
Code:
A - DateTime csvTime = new DateTime( inArray[5] ) ; //inArray[x] is loaded from a file and has this format: 2017-02-10T01:09:00Z
B - new DateTime(DateTimeZone.UTC)
Question - How do I load inArray[5] so it is truly in UTC and compares with B accurately?
Thanks in advance and yes I have looked at 100s of posts on this. I have yet to find a post that tells me the syntax for A above, how to store an external ISO 8601 format date in UTC internal. I have played around with TimeZone set default to force Joda/JVMP default TZ but that then puts me in a position of having to specify the TimeZone which I do not want to do.
If your input is already in UTC (trailing "Z") then I suggest you to parse it this way:
DateTime dt =
ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed().parseDateTime(
"2017-02-10T01:09:00Z"
);
System.out.println(dt); // 2017-02-10T01:09:00.000Z
Otherwise you could tranform your A-DateTime to UTC by withZone(DateTimeZone.UTC).

How to get android system date and time format in app

How can i get current time format that is set in android. Like it is 12 hrs or 24 hrs. I can easily get date and time and put format on it but i want to get current format of time from android system.
Use the following:
DateFormat.is24HourFormat(context);
Source: https://developer.android.com/reference/android/text/format/DateFormat.html#is24HourFormat(android.content.Context)
If you want to get a String you should use android.text.format.DateFormat
DateFormat dateFormat = android.text.format.DateFormat.getTimeFormat(context);
String = dateFormat.format(new Date());

How to change hours data from 24-hour format to 12-hour format?

I'm retrieving hours data for places from a service (Factual). It comes to me in 24-hour format and I need to display it in 12-hour format. The data for a specific day comes like this:
"sunday\":[[\"12:00\",\"21:30\"]]
I can successfully retrieve the hours from the JSON. Then, using SimpleDateFormat, I can parse the string to a Date object. But, then I can't figure out how to convert them to 12;-hour format so that I can display them as "12:00 - 9:30" or "12:00pm - 9:30pm" rather than "12:00 - 21:30".
How can I go about doing this? Thanks!
EDIT:
By parsing the string of hours (i.e. "12:00") using SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm a");, I get an error from JSON saying that the value is unparseable. If I use just SimpleDateFormat sdf2 = new SimpleDateFormat("hh:mm");, then there's no error but I can't get things to show up in 12-hour format.
If you look in the simple date format syntax docs you will find that 'h' is used for 12-hour time and 'a' is used for AM/PM. You will need to extract the two times using substring before putting them through the dateformatters.
http://developer.android.com/reference/java/text/SimpleDateFormat.html
SimpleDateFormat in = new SimpleDateFormat("<input format goes here>");
Date d = in.parse(INPUT_DATE_STRING);
SimpleDateFormat out = new SimpleDateFormat("<output format goes here>");
String outDate = out.format(d);
Try this:
//Char sequence for a 12 hour format.
CharSequence DEFAULT_FORMAT_12_HOUR = "hh:mm a";
//Char sequence for a 24 hour format.
CharSequence DEFAULT_FORMAT_24_HOUR = "kk:mm";
//date is the Date object. Look for more functions in format.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, date);
Let me know if it works. If you have any issue check the Date you are sending.
//This should give you the default time on the device. To show that it works.
DateFormat.format(DEFAULT_FORMAT_12_HOUR, Calendar.getInstance());

Extract UTC time and convert it to Unix time format

I have tow issues :
1- get UTC time from Android device
2- Convert it to UNIX time format ( millseconds )
I appreciated any feedback can help
I believe this should work:
Date now = Calendar.getInstance().getTime(); // this gets you current time as a Date
long milis = now.getTime(); // this gets you current time in UNIX format

Categories

Resources