Questions tagged [timeunit]
55 questions
162
votes
8 answers
How to convert nanoseconds to seconds using the TimeUnit enum?
How to convert a value from nanoseconds to seconds?
Here's the code segment:
import java.io.*;
import java.util.concurrent.*;
..
class Stamper {
public static void main (String[] args) {
long start = System.nanoTime();
//some try with…

phill
- 13,434
- 38
- 105
- 141
8
votes
5 answers
How to get decimal result when converting nanosecond to millisecond?
Using TimeUnit, how can I convert 665477 nanosecond to 0.665477 millisecond?
long t = TimeUnit.MILLISECONDS.convert(665477L, TimeUnit.NANOSECONDS);
This always gives 0 but I need decimal points precision.

Petter Bjao
- 327
- 1
- 3
- 10
7
votes
3 answers
TimeUnit conversion from Milliseconds to Days not working for me
I'm trying to get the difference of two timestamps in days, and TimeUnit is returning completely incorrect results for me.
Here is my code:
long ts = 1522242239952L;
long now = 1527274162820L;
long difference = now - ts;
int ageInDays = (int)…

Ahmad
- 12,886
- 30
- 93
- 146
7
votes
1 answer
Is there a way to obtain a milliseconds to days conversion without losing precision and without the need to write the mathematical formula in Java?
I was using TimeUnit.MILLISECONDS.toDays(ms) to convert a millisecond time quantity to days but reading the JavaDoc I noticed it's based on .convert() and loses precision
Convert the given time duration in the given unit to this unit. Conversions…

sarbuLopex
- 1,777
- 1
- 15
- 22
7
votes
6 answers
Generating all days between 2 given dates in Java
I'm trying to get an array of Dates, while my input is a 'from'/'to' structure.
So my input is:
String date1 = "2014-01-01";
String date2 = "2014-05-01";
My output should be an Arraylist with all dates between date1 and date2.
I've already looked…

Camelaria
- 284
- 6
- 23
7
votes
4 answers
Java TimeUnit.MILLISECONDS.toDays() gives wrong result
I'm trying to calculate the difference between two days in amount of days. For some reason comparing 01-03-2013 and 01-04-2013 gives the result 30, as does comparing 01-03-2013 and 31-03-2013
Calendar cal =…

Morri
- 571
- 5
- 20
5
votes
3 answers
How to convert from long+TimeUnit to Duration?
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…

Adam Kotwasinski
- 4,377
- 3
- 17
- 40
5
votes
2 answers
Android Work Manager: How to enqueue Jobs once a month
following this question, I looked into Android Job Scheduling and it seems like Work Manager is the latest tool if I want to do something once every month. I built a Worker class and activated it with this snippet in my activity:
PeriodicWorkRequest…

Apfelsaft23
- 316
- 2
- 23
5
votes
3 answers
What Java library object encapsulate a number and `TimeUnit`?
I want to pass around a time number and the TimeUnit it is in.
long number = some number;
TimeUnit timeUnit = some arbitrary time unit
What can hold both the time and timeUnit in one Object from the Java libraries?

The Coordinator
- 13,007
- 11
- 44
- 73
4
votes
4 answers
How do i set up a DelayQueue's Delay
I'm just starting out coding in java i'm in struggling with setting up a DelayQueue,
I wanted to have it so,
DelayQueue queue = new DelayQueue();
If (counter > 0){
queue.offer(Integer, *A custom delay*)
} Else {
queue.offer(Integer, *A different…

Pstie
- 85
- 1
- 2
- 4
4
votes
1 answer
Does TimeUnit chosen affect precision of Java utility classes?
When I use a ScheduledExecutor with a TimeUnit.MINUTES and value 3, the job gets executed with delays ranging from 3 minutes to around 3 minutes and 10 seconds. Yes, I am using scheduleWithFixedDelay(), but the job should run for only a few…

Riku
- 43
- 3
3
votes
2 answers
Passing TimeUnits and converting to milliseconds
How can I make an API for a class that the user passes a TimeUnit e.g. minutes, seconds, hours and a number and keep the millisec value internally in the class.
The following seems the only way?
void someMethodDays(int numOfDays) {
…

Jim
- 3,845
- 3
- 22
- 47
3
votes
2 answers
Double Timeunit Conversion
TimeUnit.toSeconds works well when the result is > 1. However, because we're dealing with longs, if the result is between 0 and 1 exclusive, we get 0 instead. Is the a java SDK function that handles double conversions for us?
I understand that I can…

joseph
- 2,429
- 1
- 22
- 43
3
votes
3 answers
Understanding TimeUnit
I took a look into TimeUnit and toMillis() method.
public long toMillis(long paramLong)
{
throw new AbstractMethodError();
}
toMillis() method do nothing other than throw an AbstractMethodError exception.
So, How does toMillis() method…

Tran Thien Chien
- 643
- 2
- 6
- 17
2
votes
2 answers
Extract multiple TimeUnits from a String
I am trying to strip every Time Unit of a String, e.g.
The String "4w10d50m39s" would return a TimeUnit of 4 weeks, a TimeUnit of 10 days, a TimeUnit of 50 minutes, and a TimeUnit of 39 seconds.
How can I achieve this?
Context: I need them to sum…

iLalox
- 43
- 5