1

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

  • 2
    Does this answer your question? [JSR 310 :: System.currentTimeMillis() vs Instant.toEpochMilli() :: TimeZone](https://stackoverflow.com/questions/32975392/jsr-310-system-currenttimemillis-vs-instant-toepochmilli-timezone) And/or this? [Which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()](https://stackoverflow.com/questions/58705657/which-one-is-recommended-instant-now-toepochmilli-or-system-currenttimemill) And/or this? [Java current time different values in api](https://stackoverflow.com/questions/40848243/java-current-time-different-values-in-api) – Ole V.V. Jan 14 '20 at 05:55

2 Answers2

1

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.

Chandela
  • 262
  • 5
  • 18
0

For getting timestamp in millisecond just call:

//kotlin 
val tstamp = System.currentTimeMillis()
//java
long tstamp = System.currentTimeMillis();
javadroid
  • 1,421
  • 2
  • 19
  • 43
  • thanks but why didn't you used a calendar or date option to get the timestamp? –  Jan 14 '20 at 05:34
  • for simplicity can use that method. but if you need time in other time zone, use calendar object for getting time. – javadroid Jan 14 '20 at 05:43
  • So in the android device which will be preferable? –  Jan 14 '20 at 06:05
  • If you sending results to the server you should check server time also because of android device time can charge by the user. – isuru Jan 14 '20 at 06:14
  • yes, if you just want to get time stamp in handy way and not need to get in timezone. but be careful is returned timestamp in millisecond. for second need to be divided by 1000. – javadroid Jan 14 '20 at 06:15