best way to get timestamp in long in android? [duplicate] - android

This question already has answers here:
Which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()
(4 answers)
JSR 310 :: System.currentTimeMillis() vs Instant.toEpochMilli() :: TimeZone
(3 answers)
Closed 3 years ago.
I want to understand this in detail on how to get this in android and which method to follow and please explain bit more to understand in better way ?
As we have some options to get this in android and find out the best.
It will be helpful if somebody explains with code how to get this.
Thanks in advance for your help

Hi I hope this will help you
//Getting the current date
Date date = new Date();
//This method returns the time in millis
long timeMilli = date.getTime();
System.out.println("Time in milliseconds using Date class: " + timeMilli);
//creating Calendar instance
Calendar calendar = Calendar.getInstance();
//Returns current time in millis
long timeMilli2 = calendar.getTimeInMillis();
System.out.println("Time in milliseconds using Calendar: " + timeMilli2);
//Java 8 - toEpochMilli() method of ZonedDateTime
System.out.println("Getting time in milliseconds in Java 8: " +
ZonedDateTime.now().toInstant().toEpochMilli());
And output fo these options will be
Time in milliseconds using Date class: 1508484583259
Time in milliseconds using Calendar: 1508484583267
Getting time in milliseconds in Java 8: 1508484583331
if we convert those long values to the date format then all three will be the same and it will be
Input 1508484583259
Input (formatted) 1,508,484,583,259
Date (Etc/UTC) Friday, October 20, 2017 7:29:43 AM UTC
Date (GMT) Friday, October 20, 2017 7:29:43 AM GMT
Date (short/short format) 10/20/17 7:29 AM
Over here I posted only one option result but all three will be the same or you can also check it by your own on online long to date convertor.

For getting timestamp in millisecond just call:
//kotlin
val tstamp = System.currentTimeMillis()
//java
long tstamp = System.currentTimeMillis();

Related

Time until it's 10 am again

I am trying to post a notification every day at 10 am.
The notification is sent daily already, but not at 10 am, so I need to calculate the time to wait until its 10 am again for my AlarmManager.
DateFormat dateFormat = new SimpleDateFormat("HH:mm");
long target = dateFormat.parse("10:00").getTime();
I tried this, but the timestamp I get is 50 years or so ago... (I think it is the time of the first time it was 10 am after the timestamp started counting)
So how do I calculate the milliseconds to wait until it is 10 am again?
Use Calendar instead of SimpleDateFormat for time-manipulation purposes.
In order to find 10AM on the next day:
final Calendar c = Calendar.getInstance()
c.set(Calendar.HOUR_OF_DAY, 10) // For 10AM
c.add(Calendar.DATE, 1) // Add 1 to date == tomorrow
final Date d = c.getTime()
// Do what you want with the date
You should take one Calendar object and set Hour of the day 10 AM than should get time from that calendar object like calender.getTime() it will return long and it will be set as setRepeating(calender.getTime()) while setting alarm manager.

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.

How to change milliseconds to "yyyy-MM-dd'T'HH:mm:ssS" format? [duplicate]

This question already has answers here:
Convert timestamp in milliseconds to string formatted time in Java
(10 answers)
Closed 9 years ago.
I am getting time in milliseconds ("1369807669") format from JSON and i have to change that in "yyyy-MM-dd'T'HH:mm:ssS" format ... How can i do it??
I used this code but i am getting different time not the actual time.
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
DateTime = df.format(millis);
But i am getting time as "1970-12-23'T'12:54:32+0000" which is wrong...
I should get time something like this "2013-05-29'T'12:06:53+0000"
you have seconds not milliseconds you should multipy your seconds * 1000
check this for your current date and check with your json milliseconds also
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ");
df.setTimeZone(TimeZone.getTimeZone("GMT"));
String currentDate = formatter.format(new Date(System
.currentTimeMillis())));
may be you are getting wrong time in json

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

How to calculate the total number of days passed [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate date/time difference in java
I am providing the user with the option to select the date using Date Picker. Is there any in-built method using which I can calculate the duration in days wrt to user selected date and todays date.
I don't like answering this because there were millions of questions like this (use search option before posting questions). Use Joda Time. There is a Period class, which will be useful for you.
Get the difference between the two times in milliseconds. Than you can get the Days via Java's Calendar class.
Date today = new Date(); // the date of today
Date target = new Date(); // the date of when the user picks
long todayEpoch = today.getTime(); // or can use = System.currentTimeMillis();
long targetEpoch = target.getTime();
long daysInMs = targetEpoch - todayEpoch; //days in MS's
//the # of days
float days = (daysInMs/1000/60/60/12);

Categories

Resources