• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home » Resolved: How to convert from long+TimeUnit to Duration?

Resolved: How to convert from long+TimeUnit to Duration?

0
By Isaac Tonny on 17/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m migrating some signatures from f(long dur, TimeUnit timeUnit) to f(Duration duration), and would like to implement the former with the latter.
Being on Java 8, I can’t find any API to easily convert the long+TU to a Duration, the only idea that comes to me is to do something ugly like:
Or did I miss some API?

Answer:

Java 9+


You can use static method Duration.of(long, TemporalUnit).
It expects an amount as long, and a TemporalUnit, so you need to convert the TimeUnit into ChronoUnit.
Method toChronoUnit() was introduced in JDK version 9.

Java 8


With Java 8 you can translate TimeUnit into ChronoUnit using ThreeTen library’s utility method Temporals.chronoUnit​(TimeUnit)

If you have better answer, please add a comment about this, thank you!

chronounit java java-8 java-time timeunit
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: I am facing ERR_HTTP2_PROTOCOL_ERROR on my website

27/03/2023

Resolved: TypeScript does not recognize properties when instantiating interface array

27/03/2023

Resolved: How to make statement case insensitive when uploading a dta file with specified columns?

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.