Java TimeUnit Usage getNowInSeconds()

Here you can find the source of getNowInSeconds()

Description

Return the current time in seconds.

License

Apache License

Declaration

public static long getNowInSeconds() 

Method Source Code

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

import java.util.concurrent.TimeUnit;

public class Main {
    /**//from  w w  w .j a v a2s  .  c  om
     * Return the current time in seconds.
     * 
     * @return
     */
    public static long getNowInSeconds() {
        return getNowTimeUnit(TimeUnit.SECONDS);
    }

    /**
     * Get the current time in the specified time unit.
     * @param timeUnit
     * @return
     */
    public static long getNowTimeUnit(TimeUnit timeUnit) {
        switch (timeUnit) {
        case NANOSECONDS:
            return TimeUnit.MILLISECONDS
                    .toNanos(System.currentTimeMillis());
        case MICROSECONDS:
            return TimeUnit.MILLISECONDS.toMicros(System
                    .currentTimeMillis());
        case MILLISECONDS:
            return TimeUnit.MILLISECONDS.toMillis(System
                    .currentTimeMillis());
            //default is seconds
        case SECONDS:
        default:
            return TimeUnit.MILLISECONDS.toSeconds(System
                    .currentTimeMillis());
        case MINUTES:
            return TimeUnit.MILLISECONDS.toMinutes(System
                    .currentTimeMillis());
        case HOURS:
            return TimeUnit.MILLISECONDS
                    .toHours(System.currentTimeMillis());
        case DAYS:
            return TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
        }
    }
}

Related

  1. getMailServerProps(String toAddress, int smtpPort, int imapPort)
  2. getMinutes(final long milliseconds)
  3. getMinutesBetweenDates(Date begin_date, Date end_date)
  4. getMinutesBetweenDates(Date firstDate, Date secondDate)
  5. getNormalizedIntervalStartTimestamp(long intervalLengthMillis, long currentTimestamp)
  6. getOceanTime(final Date date)
  7. getPrettyTime(long duration)
  8. getQuantilesString(final Map quantilesValues)
  9. getRandomDate()