Java Code Examples for java.time.temporal.ChronoUnit#SECONDS

The following examples show how to use java.time.temporal.ChronoUnit#SECONDS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ManualExecutor.java    From java-coroutines with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private ChronoUnit toChronoUnit(final TimeUnit unit) {
	switch (unit) {
		case NANOSECONDS:
		case MICROSECONDS:
			return ChronoUnit.MICROS;
		case MILLISECONDS:
			return ChronoUnit.MILLIS;
		case SECONDS:
			return ChronoUnit.SECONDS;
		case MINUTES:
			return ChronoUnit.MINUTES;
		case HOURS:
			return ChronoUnit.HOURS;
		case DAYS:
			return ChronoUnit.DAYS;
		default:
			throw new AssertionError();
	}
}
 
Example 2
Source File: ExpiryUtils.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public static TemporalUnit jucTimeUnitToTemporalUnit(TimeUnit timeUnit) {
  switch (timeUnit) {
    case NANOSECONDS:
      return ChronoUnit.NANOS;
    case MICROSECONDS:
      return ChronoUnit.MICROS;
    case MILLISECONDS:
      return ChronoUnit.MILLIS;
    case SECONDS:
      return ChronoUnit.SECONDS;
    case MINUTES:
      return ChronoUnit.MINUTES;
    case HOURS:
      return ChronoUnit.HOURS;
    case DAYS:
      return ChronoUnit.DAYS;
    default:
      throw new AssertionError("Unkown TimeUnit: " + timeUnit);
  }
}
 
Example 3
Source File: XmlModel.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public static TemporalUnit convertToJavaTimeUnit(org.ehcache.xml.model.TimeUnit unit) {
  switch (unit) {
    case NANOS:
      return ChronoUnit.NANOS;
    case MICROS:
      return ChronoUnit.MICROS;
    case MILLIS:
      return ChronoUnit.MILLIS;
    case SECONDS:
      return ChronoUnit.SECONDS;
    case MINUTES:
      return ChronoUnit.MINUTES;
    case HOURS:
      return ChronoUnit.HOURS;
    case DAYS:
      return ChronoUnit.DAYS;
    default:
      throw new IllegalArgumentException("Unknown time unit: " + unit);
  }
}
 
Example 4
Source File: XmlModel.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
public static TemporalUnit convertToJavaTemporalUnit(org.ehcache.xml.model.TimeUnit unit) {
  switch (unit) {
    case NANOS:
      return ChronoUnit.NANOS;
    case MICROS:
    return ChronoUnit.MICROS;
    case MILLIS:
      return ChronoUnit.MILLIS;
    case SECONDS:
      return ChronoUnit.SECONDS;
    case MINUTES:
      return ChronoUnit.MINUTES;
    case HOURS:
      return ChronoUnit.HOURS;
    case DAYS:
      return ChronoUnit.DAYS;
    default:
      throw new IllegalArgumentException("Unknown time unit: " + unit);
  }
}
 
Example 5
Source File: XpGlobesPlugin.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Schedule(
	period = 1,
	unit = ChronoUnit.SECONDS
)
public void removeExpiredXpGlobes()
{
	if (!xpGlobes.isEmpty())
	{
		Instant currentTime = Instant.now();
		for (Iterator<XpGlobe> it = xpGlobes.iterator(); it.hasNext();)
		{
			XpGlobe globe = it.next();
			Instant globeCreationTime = globe.getTime();
			if (currentTime.isBefore(globeCreationTime.plusSeconds(config.xpOrbDuration())))
			{
				//if a globe is not expired, stop checking newer globes
				return;
			}
			it.remove();
		}
	}
}
 
Example 6
Source File: DateMatchers.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
private static ChronoUnit convertUnit(final TimeUnit unit) {
    switch (unit) {
    case DAYS:
        return ChronoUnit.DAYS;
    case HOURS:
        return ChronoUnit.HOURS;
    case MICROSECONDS:
        return ChronoUnit.MICROS;
    case MILLISECONDS:
        return ChronoUnit.MILLIS;
    case MINUTES:
        return ChronoUnit.MINUTES;
    case NANOSECONDS:
        return ChronoUnit.NANOS;
    case SECONDS:
        return ChronoUnit.SECONDS;
    default:
        throw new IllegalArgumentException("Unknown TimeUnit '" + unit + "'");
    }
}
 
Example 7
Source File: Temporals.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
public static ChronoUnit chronoUnit(TimeUnit unit) {
    switch (unit) {
        case NANOSECONDS:
            return ChronoUnit.NANOS;
        case MICROSECONDS:
            return ChronoUnit.MICROS;
        case MILLISECONDS:
            return ChronoUnit.MILLIS;
        case SECONDS:
            return ChronoUnit.SECONDS;
        case MINUTES:
            return ChronoUnit.MINUTES;
        case HOURS:
            return ChronoUnit.HOURS;
        case DAYS:
            return ChronoUnit.DAYS;
        default:
            throw new IllegalArgumentException("Cannot convert timeunit");
    }
}
 
Example 8
Source File: Cache.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
protected static ChronoUnit convertToChronoUnit(final TimeUnit timeUnit) {
    switch (timeUnit) {
    case NANOSECONDS:
        return ChronoUnit.NANOS;
    case MICROSECONDS:
        return ChronoUnit.MICROS;
    case SECONDS:
        return ChronoUnit.SECONDS;
    case MINUTES:
        return ChronoUnit.MINUTES;
    case HOURS:
        return ChronoUnit.HOURS;
    case DAYS:
        return ChronoUnit.DAYS;
    case MILLISECONDS:
    default:
        return ChronoUnit.MILLIS;
    }
}
 
Example 9
Source File: FindResult.java    From find with MIT License 5 votes vote down vote up
@SuppressWarnings("TypeMayBeWeakened")
private ZonedDateTime parseRelativeTime(final ZonedDateTime timeNow, final int timeAmount, final String timeUnit) {
    TemporalUnit temporalUnit = ChronoUnit.SECONDS;
    switch (timeUnit) {
        case "minute":
        case "minutes":
            temporalUnit = ChronoUnit.MINUTES;
            break;

        case "hour":
        case "hours":
            temporalUnit = ChronoUnit.HOURS;
            break;

        case "day":
        case "days":
            temporalUnit = ChronoUnit.DAYS;
            break;

        case "month":
        case "months":
            temporalUnit = ChronoUnit.MONTHS;
            break;

        case "year":
        case "years":
            temporalUnit = ChronoUnit.YEARS;
            break;
    }

    return timeNow.plus(timeAmount, temporalUnit);
}
 
Example 10
Source File: RollingFileWriter.java    From swage with Apache License 2.0 5 votes vote down vote up
/**
 * Create a RollingFileWriter as specified
 *
 * @param directory Path where the log files will be created
 * @param name Base name to use for log files
 * @param zone Time zone of timestamps on rolling file names
 * @param minuteRotation If true, rotate logs every minute, if false rotate every hour
 */
RollingFileWriter(Path directory, String name, TimeZone zone, boolean minuteRotation)
{
    if (directory == null) {
        throw new IllegalArgumentException("Directory required");
    }
    if (name == null) {
        throw new IllegalArgumentException("File base name required");
    }
    if (zone == null) {
        throw new IllegalArgumentException("Time zone required");
    }

    this.directory = directory;
    this.baseName = name;
    this.timeZone = zone.toZoneId();

    final String formatPattern = (minuteRotation ? ".yyyy-MM-dd-HH-mm" : ".yyyy-MM-dd-HH");
    this.format = DateTimeFormatter.ofPattern(formatPattern);

    if (minuteRotation) {
        rollInterval = ChronoUnit.SECONDS;
    } else {
        rollInterval = ChronoUnit.HOURS;
    }

    // Set to roll immediately on first write, to ensure file is created
    this.rollAt = Instant.now().truncatedTo(rollInterval);
}
 
Example 11
Source File: DateWrapper.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
public DateWrapper(final int year, final Month month, final int dayOfMonth, final int hour, final int minute,
		final int second) {
	wrapped = ZonedDateTime
			.of(LocalDateTime.of(year, month, dayOfMonth, hour, minute, second), ZoneId.systemDefault())
				.toInstant();
	accuracy = ChronoUnit.SECONDS;
}
 
Example 12
Source File: TimeoutClient.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * serviceD specifies a Timeout longer than the default, at 2 
 * seconds.
 * 
 * @param timeToSleepInMillis  How long should the execution take in millis
 * @return null or exception is raised
 */
@Timeout(value = 2, unit = ChronoUnit.SECONDS)
public Connection serviceD(long timeToSleepInMillis) {
    try {
        Thread.sleep(timeToSleepInMillis);
        throw new RuntimeException("Timeout did not interrupt");
    } 
    catch (InterruptedException e) {
        //expected
    }
    return null;
}
 
Example 13
Source File: TimeTrackingPlugin.java    From plugins with GNU General Public License v3.0 5 votes vote down vote up
@Schedule(period = 10, unit = ChronoUnit.SECONDS)
public void checkCompletion()
{
	boolean birdHouseDataChanged = birdHouseTracker.checkCompletion();

	if (birdHouseDataChanged)
	{
		panel.update();
	}
}
 
Example 14
Source File: Bulkhead55MethodAsynchronousRetryBean.java    From microprofile-fault-tolerance with Apache License 2.0 5 votes vote down vote up
@Override
@Bulkhead(waitingTaskQueue = 5, value = 5)
@Asynchronous
@Retry(retryOn =
{ BulkheadException.class }, delay = 1, delayUnit = ChronoUnit.SECONDS, maxRetries = 10, maxDuration=999999)
public Future test(BackendTestDelegate action) throws InterruptedException {
    Utils.log("in business method of bean " + this.getClass().getName());
    return action.perform();
}
 
Example 15
Source File: ClusteringServiceConfigurationBuilder.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private static ChronoUnit toChronoUnit(TimeUnit unit) {
  if(unit == null) {
    return null;
  }
  switch (unit) {
    case NANOSECONDS:  return ChronoUnit.NANOS;
    case MICROSECONDS: return ChronoUnit.MICROS;
    case MILLISECONDS: return ChronoUnit.MILLIS;
    case SECONDS:      return ChronoUnit.SECONDS;
    case MINUTES:      return ChronoUnit.MINUTES;
    case HOURS:        return ChronoUnit.HOURS;
    case DAYS:         return ChronoUnit.DAYS;
    default: throw new AssertionError("Unknown unit: " + unit);
  }
}
 
Example 16
Source File: TimeUtil.java    From L2jOrg with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Parses patterns like:
 * <ul>
 * <li>1min or 10mins</li>
 * <li>1day or 10days</li>
 * <li>1week or 4weeks</li>
 * <li>1month or 12months</li>
 * <li>1year or 5years</li>
 * </ul>
 * @param datePattern
 * @return {@link Duration} object converted by the date pattern specified.
 * @throws IllegalStateException when malformed pattern specified.
 */
public static Duration parseDuration(String datePattern)
{
	final int index = findIndexOfNonDigit(datePattern);
	if (index == -1)
	{
		throw new IllegalStateException("Incorrect time format given: " + datePattern);
	}
	try
	{
		final int val = Integer.parseInt(datePattern.substring(0, index));
		final String type = datePattern.substring(index);
		final ChronoUnit unit;
		switch (type.toLowerCase())
		{
			case "sec":
			case "secs":
			{
				unit = ChronoUnit.SECONDS;
				break;
			}
			case "min":
			case "mins":
			{
				unit = ChronoUnit.MINUTES;
				break;
			}
			case "hour":
			case "hours":
			{
				unit = ChronoUnit.HOURS;
				break;
			}
			case "day":
			case "days":
			{
				unit = ChronoUnit.DAYS;
				break;
			}
			case "week":
			case "weeks":
			{
				unit = ChronoUnit.WEEKS;
				break;
			}
			case "month":
			case "months":
			{
				unit = ChronoUnit.MONTHS;
				break;
			}
			case "year":
			case "years":
			{
				unit = ChronoUnit.YEARS;
				break;
			}
			default:
			{
				unit = ChronoUnit.valueOf(type);
				if (unit == null)
				{
					throw new IllegalStateException("Incorrect format: " + type + " !!");
				}
			}
		}
		return Duration.of(val, unit);
	}
	catch (Exception e)
	{
		throw new IllegalStateException("Incorrect time format given: " + datePattern + " val: " + datePattern.substring(0, index));
	}
}
 
Example 17
Source File: RetryConfigBean.java    From microprofile-fault-tolerance with Apache License 2.0 4 votes vote down vote up
@Retry(maxRetries = 5, delay = 2, delayUnit = ChronoUnit.SECONDS, jitter = 0)
public void serviceDelay() {
    throw new TestException();
}
 
Example 18
Source File: AlchemyRoomTimer.java    From plugins with GNU General Public License v3.0 4 votes vote down vote up
AlchemyRoomTimer(final Plugin plugin)
{
	super(RESET_PERIOD, ChronoUnit.SECONDS, getResetImage(), plugin);
	this.setTooltip("Time until items swap");
}
 
Example 19
Source File: Duration.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a copy of this {@code Duration} truncated to the specified unit.
 * <p>
 * Truncating the duration returns a copy of the original with conceptual fields
 * smaller than the specified unit set to zero.
 * For example, truncating with the {@link ChronoUnit#MINUTES MINUTES} unit will
 * round down towards zero to the nearest minute, setting the seconds and
 * nanoseconds to zero.
 * <p>
 * The unit must have a {@linkplain TemporalUnit#getDuration() duration}
 * that divides into the length of a standard day without remainder.
 * This includes all
 * {@linkplain ChronoUnit#isTimeBased() time-based units on {@code ChronoUnit}}
 * and {@link ChronoUnit#DAYS DAYS}. Other ChronoUnits throw an exception.
 * <p>
 * This instance is immutable and unaffected by this method call.
 *
 * @param unit the unit to truncate to, not null
 * @return a {@code Duration} based on this duration with the time truncated, not null
 * @throws DateTimeException if the unit is invalid for truncation
 * @throws UnsupportedTemporalTypeException if the unit is not supported
 * @since 9
 */
public Duration truncatedTo(TemporalUnit unit) {
    Objects.requireNonNull(unit, "unit");
    if (unit == ChronoUnit.SECONDS && (seconds >= 0 || nanos == 0)) {
        return new Duration(seconds, 0);
    } else if (unit == ChronoUnit.NANOS) {
        return this;
    }
    Duration unitDur = unit.getDuration();
    if (unitDur.getSeconds() > LocalTime.SECONDS_PER_DAY) {
        throw new UnsupportedTemporalTypeException("Unit is too large to be used for truncation");
    }
    long dur = unitDur.toNanos();
    if ((LocalTime.NANOS_PER_DAY % dur) != 0) {
        throw new UnsupportedTemporalTypeException("Unit must divide into a standard day without remainder");
    }
    long nod = (seconds % LocalTime.SECONDS_PER_DAY) * LocalTime.NANOS_PER_SECOND + nanos;
    long result = (nod / dur) * dur;
    return plusNanos(result - nod);
}
 
Example 20
Source File: TimePeriod.java    From java-timeseries with MIT License 2 votes vote down vote up
/**
 * Create and return a new TimePeriod representing one second.
 *
 * @return a new TimePeriod representing one second.
 */
public static TimePeriod oneSecond() {
  return new TimePeriod(ChronoUnit.SECONDS, 1);
}