0

I would like the current date in ms.

I am with the API23, I can't use Instant.now().

tvDateUTC.setText(Instant.now().toString());

I would like to retrieve something that looks like this:

1549536499906
ETO
  • 6,970
  • 1
  • 20
  • 37
Killian
  • 73
  • 1
  • 9

4 Answers4

1
java.lang.System.currentTimeMillis()

The java.lang.System.currentTimeMillis() method returns the current time in milliseconds.The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger.

For example, many operating systems measure time in units of tens of milliseconds.

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
0

Problem Solved:

private String getDate(){
    String ts = String.valueOf(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()));
    return ts;
}

Sorry for the inconvenience!

Killian
  • 73
  • 1
  • 9
0

Using .getTime() method will return date in milliseconds.

eg.

Date currentDate = new Date();

System.out.println(currentDate.getTime()); 

Alternatively you can use

Date currentTime = Calendar.getInstance().getTime();
ViraatS
  • 1
  • 1
0

tl;dr

org.threeten.bp.Instant  // Represent a moment in UTC with a resolution of nanoseconds using this back-port to early Java.  
.now()                   // Capture the current moment in UTC. 
.toEpochMilli()          // Determine the count of milliseconds from the first moment of 1970 in UTC to this recorded moment. 

java.time

The legacy date-time types are terrible. So you should definitely be using their replacement, the java.time classes built into Java 8 and later, and in Android 26 and later.

Most of the java.time functionality is back-ported to Java 6 and Java 7 in the ThreeTen-Backport project.

Further adapted to early Android in the ThreeTenABP project.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154