Java TimeUnit Convert getRemainingTimeToday(final TimeUnit timeUnit)

Here you can find the source of getRemainingTimeToday(final TimeUnit timeUnit)

Description

get Remaining Time Today

License

Apache License

Declaration

public static long getRemainingTimeToday(final TimeUnit timeUnit) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

import java.util.concurrent.TimeUnit;

public class Main {

    public static long getRemainingTimeToday(final TimeUnit timeUnit) {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        long durationMills = todayEnd.getTimeInMillis() - System.currentTimeMillis();
        return millisecondsTo(durationMills, timeUnit);
    }// w w  w. j a v a2 s.  co m

    public static long millisecondsTo(long milliseconds, final TimeUnit timeUnit) {
        switch (timeUnit) {
        case MILLISECONDS:
            return TimeUnit.MILLISECONDS.toMillis(milliseconds);
        case NANOSECONDS:
            return TimeUnit.MILLISECONDS.toNanos(milliseconds);
        case MICROSECONDS:
            return TimeUnit.MILLISECONDS.toMicros(milliseconds);
        case SECONDS:
            return TimeUnit.MILLISECONDS.toSeconds(milliseconds);
        case MINUTES:
            return TimeUnit.MILLISECONDS.toMinutes(milliseconds);
        case HOURS:
            return TimeUnit.MILLISECONDS.toHours(milliseconds);
        case DAYS:
            return TimeUnit.MILLISECONDS.toDays(milliseconds);
        default:
            throw new IllegalArgumentException();
        }
    }
}

Related

  1. convertToMillis(long waitTime, TimeUnit timeUnit)
  2. convertToMilliseconds(TimeUnit timeUnit, long seed)
  3. convertToSecond(int interval, TimeUnit unit)
  4. durationToMillis(final long val, final TimeUnit unit)
  5. getDateRelativeToNow(TimeUnit timeUnit, long amount)
  6. hoursToUnit(double hours, TimeUnit destinationUnit)
  7. millisecondsTo(long milliseconds, final TimeUnit timeUnit)
  8. millisToDuration(final int val, final TimeUnit unit)
  9. stringToTimeUnit(String str)