Extract UTC time and convert it to Unix time format - android

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

Related

Android - Convert user selected time to UTC and vice versa

I'm working on Android application, and I want to convert user-selected local time (device time) into UTC seconds. After that, I have to convert it again and display in the device's time zone. Can anyone suggest how to do this in Kotlin?
I want to convert user-selected local time (device time) into UTC seconds
You're thinking about this incorrectly. Timestamps, including device time, do not have a time zone. All timestamps are seconds since Jan 1 1970 00:00 UTC, regardless of device time zone. If the user selects a time, and you have that time as a timestamp, it's already in the right format. You can think of it as "UTC seconds," since it's based on a time in UTC, but there's no such thing as timestamps that aren't in such "UTC seconds."
The only time you need a time zone is for converting to a date, displaying it to a user, etc.
Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default FORMAT locale
fun localToGMT(): Date? {
val date = Date()
val sdf = getDateInstance()
sdf.timeZone = TimeZone.getTimeZone("UTC")
return DateFormat.getDateInstance().parse(sdf.format(date))
}
fun gmttoLocalDate(date: Date):Date? {
val timeZone = Calendar.getInstance().getTimeZone().getID();
val local = Date(date.getTime() + TimeZone.getTimeZone(timeZone).getOffset(date.getTime()));
return local
}

Android SimpleDateFormat is not parsing hours

when parsing using SimpleDateFormat the milliseconds of date object is not correct
Date locally=new SimpleDateFormat("H:mm").parse("03:00");
the locally milliseconds is 3600000 but the true result must be 3*60*60*1000=10800000
Parsing a timezone-less information like "03:00" to a global type like Date is always timezone-relevant. In your case parsing uses the default timezone of your system which has obviously an extra offset of two hours so you get the local time reduced by two hours, resulting in an UTC-timestamp of 1970-01-01T01:00Z.
It seems you want to interprete the input as kind of duration. Although a Date-object is naturally NOT a duration you might slightly misuse the Date-type this way:
SimpleDateFormat sdf = new SimpleDateFormat("H:mm");
sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // interprete input in UTC (zero offset)
Date d = sdf.parse("03:00"); // timestamp equivalent to 1970-01-01T03:00Z
long durationInMillis = d.getTime(); // 10800000 ms relative to UNIX epoch

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

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;

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

Format of data-time storage

I am sending Sms from emulator control to emulator.. but it shows time in some different format. Can Anyone Help me understand that code or format. Here is the pic
Its probably using the System.currentTimeMillis() function for accessing the time, what returns the current time in milliseconds. If you want your date formatted, then use:
long time = System.currentTimeMillis();
String timeString = new Date(time).toLocaleString();
Or if you just need the time part from it, like in the example you have shown, then:
SimpleDateFormat formater = new SimpleDateFormat("h:mm a");
String timeString = formater.format(new Date(time)); //time is the current time as a long value;
As for how the date is stored:
System.currentTimeMillis()
Returns the difference, measured in milliseconds, between the current
time and midnight, January 1, 1970 UTC.
This means, that the long number you get, is the passed milliseconds since January 1, 1970.
From this value its pretty easy to count the current year, month day, etc...
As you can see in my previous example, you can conver this long value to a Date object, by passing it to the Date() constructor, and you can convert a Date object to a long value using:
long time = dateObject.getTime();
I hope this helps!

Categories

Resources