user3248346
user3248346

Reputation:

Format UTC Date Time

On my machine, when I call Instant.now I get the following:

scala> import java.time._
import java.time._

scala> Instant.now
res1: java.time.Instant = 2017-03-03T07:05:41.192Z

It returns the fractional part of the second to the thousandths i.e. .192Z. I would like to only return it to the hundreths, so in this case .19Z. How can I do that?

I tried using DateFormatter but it blows up with the I put the T in the pattern string.

Upvotes: 2

Views: 937

Answers (3)

Ömer Erden
Ömer Erden

Reputation: 8793

You can use DateTimeFormatter with your custom pattern.

Sample:

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SS'Z'")
    .withZone(ZoneId.systemDefault())
    .format(Instant.now());

Upvotes: 4

Sunil
Sunil

Reputation: 508

This worked for me -

String format = "yyyy-MM-dd'T'HH:mm:ss.ssZ";
System.out.println(OffsetDateTime.now().format(DateTimeFormatter.ofPattern(format)));
System.out.println(ZonedDateTime.now().format(DateTimeFormatter.ofPattern(format)));

Output:

2019-04-03T13:23:03.03+0530
2019-04-03T13:23:03.03+0530

Upvotes: 0

Bart
Bart

Reputation: 265

You can call:

Instant.now().getMillis(), this should return 192 as a Long

Upvotes: -1

Related Questions