TimeUnit
is an enum that deals with different units of time and operations related to it. For example, TimeUnit
has different units of time like NANOSECONDS
, MICROSECONDS
, MILLISECONDS
, SECONDS
, MINUTES
, HOURS
, and DAYS
.
The toMinutes()
method of the TimeUnit
class is used to convert the time represented by the TimeUnit
object to minutes of
TimeUnit
enumThe TimeUnit
enum is defined in the java.util.concurrent
package. Use the import statement below to import the TimeUnit
enum.
import java.util.concurrent.TimeUnit;
public long toMinutes(long duration)
duration
: the duration (generally in milliseconds) to be converted to minutes.The method returns the converted duration to minutes. The method returns Long.MIN_VALUE
if the conversion overflows negatively, and Long.MAX_VALUE
if it overflows positively.
In the code below, we use the Calendar
class to get the current time in milliseconds.
Next, we create a TimeUnit
object with the help of MILLISECONDS
, which is a time unit that represents one-thousandth of a second.
We pass the current time in milliseconds to the toMinutes
method of the TimeUnit
object to get the time in minutes.
import java.util.Calendar;import java.util.concurrent.TimeUnit;class Main {public static void main(String[] args) {Calendar calendar = Calendar.getInstance();long currentTimeMilliseconds = calendar.getTimeInMillis();TimeUnit time = TimeUnit.MILLISECONDS;System.out.println("The time " + currentTimeMilliseconds + " milliSeconds in minutes = " + time.toMinutes(currentTimeMilliseconds));}}