Java Code Examples for java.time.temporal.ChronoUnit#valueOf()

The following examples show how to use java.time.temporal.ChronoUnit#valueOf() . 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: TimeConfiguration.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
/**
 * Parse raw json content into schedule instance.
 *
 * @param parser json based content parser
 * @return schedule instance
 * @throws IOException IOException if content can't be parsed correctly
 */
public static TimeConfiguration parse(XContentParser parser) throws IOException {
    long interval = 0;
    ChronoUnit unit = null;
    String scheduleType = null;

    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.currentToken(), parser::getTokenLocation);
    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        scheduleType = parser.currentName();
        parser.nextToken();
        switch (scheduleType) {
            case PERIOD_FIELD:
                while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
                    String periodFieldName = parser.currentName();
                    parser.nextToken();
                    switch (periodFieldName) {
                        case INTERVAL_FIELD:
                            interval = parser.longValue();
                            break;
                        case UNIT_FIELD:
                            unit = ChronoUnit.valueOf(parser.text().toUpperCase(Locale.ROOT));
                            break;
                        default:
                            break;
                    }
                }
                break;
            default:
                break;
        }
    }
    if (PERIOD_FIELD.equals(scheduleType)) {
        return new IntervalTimeConfiguration(interval, unit);
    }
    throw new IllegalArgumentException("Find no schedule definition");
}
 
Example 2
Source File: TimeUtils.java    From brein-time-utilities with Apache License 2.0 5 votes vote down vote up
public static ChronoUnit convertToChronoUnit(final String chronoUnit) {
    if (chronoUnit == null) {
        return null;
    }

    try {
        return ChronoUnit.valueOf(chronoUnit.toUpperCase());
    } catch (final IllegalArgumentException e) {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Couldn't get value of " + chronoUnit, e);
        }
        return null;
    }
}
 
Example 3
Source File: Timeshift.java    From chronix.server with Apache License 2.0 5 votes vote down vote up
/**
 * @param args the first value is the amount, e.g 10, the second one is the unit, e.g HOURS
 */
@Override
public void setArguments(String[] args) {
    this.amount = Long.parseLong(args[0]);
    this.unit = ChronoUnit.valueOf(args[1].toUpperCase());
    this.shift = unit.getDuration().toMillis() * amount;
}
 
Example 4
Source File: MovingAverage.java    From chronix.server with Apache License 2.0 5 votes vote down vote up
/**
 * @param args the first value is the time span e.g. 5, 10, the second one is the unit of the time span
 */
@Override
public void setArguments(String[] args) {
    this.timeSpan = Long.parseLong(args[0]);
    this.unit = ChronoUnit.valueOf(args[1].toUpperCase());
    this.windowTime = unit.getDuration().toMillis() * timeSpan;
}
 
Example 5
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 6
Source File: OlympicModel2.java    From egads with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Default Ctor
 * @param config A non-null and non-empty properties map.
 * @throws IllegalArgumentException if a required property is missing.
 * @throws NumberFormatException if a numeric property could not be parsed.
 */
public OlympicModel2(final Properties config) {
    super(config);
    
    // TODO - some additional validation around distance being greater
    // or equal to the window size.
    String temp = config.getProperty("WINDOW_SIZE");
    if (temp == null || temp.isEmpty()) {
        throw new IllegalArgumentException("WINDOW_SIZE is required, "
                + "e.g. 1 or 5");
    }
    windowSize = Long.parseLong(temp);
    temp = config.getProperty("WINDOW_SIZE_UNITS");
    if (temp == null || temp.isEmpty()) {
        throw new IllegalArgumentException("WINDOW_SIZE_UNITS is required, "
                + "e.g. MINUTES OR HOURS");
    }
    windowUnits = ChronoUnit.valueOf(temp.toUpperCase()); 
    
    temp = config.getProperty("INTERVAL");
    if (temp == null || temp.isEmpty()) {
        throw new IllegalArgumentException("INTERVAL is required, "
                + "e.g. 1 or 5");
    }
    interval = Long.parseLong(temp);
    temp = config.getProperty("INTERVAL_UNITS");
    if (temp == null || temp.isEmpty()) {
        throw new IllegalArgumentException("INTERVAL_UNITS is required, "
                + "e.g. MINUTES OR HOURS");
    }
    intervalUnits = ChronoUnit.valueOf(temp.toUpperCase());
    
    temp = config.getProperty("WINDOW_DISTANCE");
    if (temp == null || temp.isEmpty()) {
        throw new IllegalArgumentException("WINDOW_DISTANCE is required, "
                + "e.g. 1 or 5");
    }
    windowDistanceInterval = Long.parseLong(temp);
    
    temp = config.getProperty("WINDOW_DISTANCE_UNITS");
    if (temp == null || temp.isEmpty()) {
        throw new IllegalArgumentException("WINDOW_DISTANCE_UNITS is "
                + "required, e.g. MINUTES OR HOURS");
    }
    windowDistanceIntervalUnits = ChronoUnit.valueOf(temp.toUpperCase());
    
    temp = config.getProperty("WINDOW_AGGREGATOR");
    if (temp == null || temp.isEmpty()) {
        windowAggregator = "AVG";
    } else {
        temp = temp.toUpperCase();
        if (!(temp.equals("AVG") || temp.equals("MIN") 
            || temp.equals("MAX") || temp.equals("SUM") 
            || temp.equals("COUNT") || temp.equals("WAVG")
            || temp.equals("MEDIAN"))) {
            throw new IllegalArgumentException("The window aggregator was"
                    + " not implemented: " + temp);
        }
        windowAggregator = temp;
    }
    
    temp = config.getProperty("MODEL_START");
    if (temp == null || temp.isEmpty()) {
        throw new IllegalArgumentException("MODEL_START is required, "
                + "e.g. 1474756200");
    }
    modelStartEpoch = Long.parseLong(temp);
    
    pastWindows = Integer.parseInt(config.getProperty(
            "HISTORICAL_WINDOWS", "1"));
    weighting = Boolean
            .parseBoolean(config.getProperty("ENABLE_WEIGHTING", "false"));
    futureWindows = Integer
            .parseInt(config.getProperty("FUTURE_WINDOWS", "1"));
    drop_highest = Integer
            .parseInt(config.getProperty("NUM_TO_DROP_HIGHEST", "0"));
    drop_lowest = Integer
            .parseInt(config.getProperty("NUM_TO_DROP_LOWEST", "0"));

    zone = ZoneId.of(config.getProperty("TIMEZONE", "UTC"));
    
    windowTimes = new ZonedDateTime[pastWindows];
    indices = new int[pastWindows];
    model = Lists.newArrayList();
}
 
Example 7
Source File: DependencyGraphConfiguration.java    From kieker with Apache License 2.0 4 votes vote down vote up
public DependencyGraphConfiguration(final String importDirectory, final String timeUnitOfRecods,
		final String exportDirectory) {
	this(new File(importDirectory), ChronoUnit.valueOf(timeUnitOfRecods), new File(exportDirectory));
}