-1

I want to migrate this code from Joda-Time to its replacement, java.time (JSR 310).

Specifically I want to use ZonedDateTime.

Old code:

DateTime startTime = new DateTime();
DateTime stopTime = new DateTime();

durationMilli = Math.max(1, (stopTime.getMillis() - startTime.getMillis()));

to

ZonedDateTime startTime = ZonedDateTime.now();
ZonedDateTime stopTime = ZonedDateTime.now();
    
durationMilli = Math.max(1, (stopTime.getMillis() - startTime.getMillis()));

I have the following questions.

How I can set ZonedDateTime.now() to empty value? As you can see stopTime should be empty. Send issue is that method getMillis is missing. How I can get milliseconds?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 5
    Sorry, I cannot see "`stopTime` should be empty" - what do you understand as "empty value" ? || Last part of question: `Duration.between(start, end).toMillis()` – user16320675 Apr 16 '23 at 21:21
  • 2
    Please define what you mean by "empty value". – Slaw Apr 16 '23 at 21:44

2 Answers2

3

tl;dr

How I can set ZonedDateTime.now() to empty value?

Do as you would do for an object reference of any other class. Either:

  • null
  • Optional.empty

How I can get milliseconds?

myZonedDateTime.toInstant().toEpochMilli()

Details

Yes, ZonedDateTime replaces DateTime

The DateTime class in Joda-Time represents a moment, a specific point on the timeline, as viewed through a specific time zone.

new org.joda.time.DateTime() captures the current moment as seen in the current default time zone. It's equivalent in java.time is ZonedDateTime.now().

Make time zone explicit

By the way, I find it more clear to specify that you know and want the JVM’s current default time zone. Like this: ZonedDateTime.now( ZoneId.systemDefault() ) rather than rely implicitly on the default.

Empty?

You said:

As you can see stopTime should be empty.

No, I do not see that at all. Where in your code is there any kind of empty value?

A date-time object such as ZonedDateTime always has a value. Having a value is its job, specifically: representing a moment as seen through a time zone. So there is no "empty" value. If you want to represent having no known moment, use either:

  • null
  • Optional

… with Optional being recommended. See Guide To Java 8 Optional by baeldung.

Optional< ZonedDateTime > empty = Optional.empty();

Epoch reference as a placeholder

Alternatively, if you want to use a specific moment as a flag or placeholder, I would suggest using the epoch reference defined in java.time: the first moment of 1970 with an offset of zero hours-minutes-seconds from UTC, 1970-01-01T00:00Z.

The java.time.Instant class has a predefined constant for that value: Instant.EPOCH.

Adjusting between UTC and a time zone

You can adjust an Instant in UTC to a ZonedDateTime in a particular time zone.

Instant instant = Instant.EPOCH ;
ZoneId z = Zone.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

And go back again, moving from time zone to UTC (zero offset).

Instant instant = zdt.toInstant() ;

Count of milliseconds since epoch reference

You may interrogate the Instant object for a count of milliseconds since the epoch reference of 1970-01-01T00:00Z. Be aware of possible data loss, as an Instant object may have microseconds or nanoseconds that you’ll be ignoring.

long millisSinceEpoch = instant.toEpochMilli() ;

And back the other direction:

Instant instant = Instant.ofEpochMilli( millisSinceEpoch ) ;

ISO 8601

I recommend against tracking time as a count from epoch. Such numbers are meaningless to humans, as we cannot determine a moment from a mere integer number. That means errors can slip by unnoticed. And debugging is more difficult. Also, there is the Y2K38 Problem.

Instead, when storing or exchanging date-time values textually, use standard ISO 8601 formats.

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

I dont think there is a need to set ZonedDateTime.now() to empty value. You can use zonedDateTime.toInstant() and then instant.toEpochMilli() to get it in milliseconds.

Renjith
  • 3,274
  • 19
  • 39