Java Code Examples for java.time.format.DateTimeFormatter#ISO_DATE_TIME

The following examples show how to use java.time.format.DateTimeFormatter#ISO_DATE_TIME . 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: JdbcCommon.java    From nifi with Apache License 2.0 6 votes vote down vote up
public static DateTimeFormatter getDateTimeFormatter(String pattern) {
    switch(pattern) {
        case "BASIC_ISO_DATE": return DateTimeFormatter.BASIC_ISO_DATE;
        case "ISO_LOCAL_DATE": return DateTimeFormatter.ISO_LOCAL_DATE;
        case "ISO_OFFSET_DATE": return DateTimeFormatter.ISO_OFFSET_DATE;
        case "ISO_DATE": return DateTimeFormatter.ISO_DATE;
        case "ISO_LOCAL_TIME": return DateTimeFormatter.ISO_LOCAL_TIME;
        case "ISO_OFFSET_TIME": return DateTimeFormatter.ISO_OFFSET_TIME;
        case "ISO_TIME": return DateTimeFormatter.ISO_TIME;
        case "ISO_LOCAL_DATE_TIME": return DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        case "ISO_OFFSET_DATE_TIME": return DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        case "ISO_ZONED_DATE_TIME": return DateTimeFormatter.ISO_ZONED_DATE_TIME;
        case "ISO_DATE_TIME": return DateTimeFormatter.ISO_DATE_TIME;
        case "ISO_ORDINAL_DATE": return DateTimeFormatter.ISO_ORDINAL_DATE;
        case "ISO_WEEK_DATE": return DateTimeFormatter.ISO_WEEK_DATE;
        case "ISO_INSTANT": return DateTimeFormatter.ISO_INSTANT;
        case "RFC_1123_DATE_TIME": return DateTimeFormatter.RFC_1123_DATE_TIME;
        default: return DateTimeFormatter.ofPattern(pattern);
    }
}
 
Example 2
Source File: ContainerListCommands.java    From mobycraft with Apache License 2.0 6 votes vote down vote up
private List<Container> convertJobsToContainers(List<Job> jobs) {
	List<Container> containers = new ArrayList<>();
	for (Job job: jobs) {
		for (Job.Task task : job.getTasks()) {
			DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
			ZonedDateTime date = ZonedDateTime.parse(task.getStartedAt(), formatter);
			long startedAtLong = date.toInstant().getEpochSecond() * 1000;

			String names[] = {task.getId()};
			// TODO: parse the states
			Container c = new Container(job.getEntryPoint(), startedAtLong, task.getId(), job.getApplicationName(), names, ContainerStatus.RUNNING, "running");
			containers.add(c);
		}
	}
	return containers;
}
 
Example 3
Source File: Jsr310DateTimeFormatAnnotationFormatterFactory.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);

	// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
	if (formatter == DateTimeFormatter.ISO_DATE) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_TIME;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_DATE_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		}
	}

	return new TemporalAccessorPrinter(formatter);
}
 
Example 4
Source File: Jsr310DateTimeFormatAnnotationFormatterFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
	DateTimeFormatter formatter = getFormatter(annotation, fieldType);

	// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
	if (formatter == DateTimeFormatter.ISO_DATE) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_TIME;
		}
	}
	else if (formatter == DateTimeFormatter.ISO_DATE_TIME) {
		if (isLocal(fieldType)) {
			formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
		}
	}

	return new TemporalAccessorPrinter(formatter);
}
 
Example 5
Source File: UserLocalizationDateAdapter.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public Date read(JsonReader in) throws IOException {
    if (in.peek().equals(JsonToken.NULL)) {
        in.nextNull();
        return null;
    }
    String dateString = in.nextString();

    DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_DATE_TIME;
    OffsetDateTime offsetDateTime = OffsetDateTime.parse(
            dateString, DateTimeFormatter.ISO_DATE_TIME
    );

    return Date.from(Instant.from(offsetDateTime));
}
 
Example 6
Source File: PutSQL.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private DateTimeFormatter getDateTimeFormatter(String pattern) {
    switch(pattern) {
        case "BASIC_ISO_DATE": return DateTimeFormatter.BASIC_ISO_DATE;
        case "ISO_LOCAL_DATE": return DateTimeFormatter.ISO_LOCAL_DATE;
        case "ISO_OFFSET_DATE": return DateTimeFormatter.ISO_OFFSET_DATE;
        case "ISO_DATE": return DateTimeFormatter.ISO_DATE;
        case "ISO_LOCAL_TIME": return DateTimeFormatter.ISO_LOCAL_TIME;
        case "ISO_OFFSET_TIME": return DateTimeFormatter.ISO_OFFSET_TIME;
        case "ISO_TIME": return DateTimeFormatter.ISO_TIME;
        case "ISO_LOCAL_DATE_TIME": return DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        case "ISO_OFFSET_DATE_TIME": return DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        case "ISO_ZONED_DATE_TIME": return DateTimeFormatter.ISO_ZONED_DATE_TIME;
        case "ISO_DATE_TIME": return DateTimeFormatter.ISO_DATE_TIME;
        case "ISO_ORDINAL_DATE": return DateTimeFormatter.ISO_ORDINAL_DATE;
        case "ISO_WEEK_DATE": return DateTimeFormatter.ISO_WEEK_DATE;
        case "ISO_INSTANT": return DateTimeFormatter.ISO_INSTANT;
        case "RFC_1123_DATE_TIME": return DateTimeFormatter.RFC_1123_DATE_TIME;
        default: return DateTimeFormatter.ofPattern(pattern);
    }
}
 
Example 7
Source File: TimeParser.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * A Helper function to help you convert various string represented time
 * definitions to an absolute Instant.
 *
 * @param time a string that represents an instant in time
 * @return the parsed Instant or null
 * @deprecated Use {@link TimestampFormats#parse(String)}
 */
@Deprecated
public static Instant getInstant(String time) {
    if (time.equalsIgnoreCase(NOW)) {
        return Instant.now();
    } else {
        Matcher nUnitsAgoMatcher = timeQuantityUnitsPattern.matcher(time);
        while (nUnitsAgoMatcher.find()) {
            return Instant.now().minus(parseDuration(nUnitsAgoMatcher.group(1)));
        }
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        return LocalDateTime.parse(time, formatter).atZone(TimeZone.getDefault().toZoneId()).toInstant();
    }
}
 
Example 8
Source File: EntityRSQLNodeTraveller.java    From pnc with Apache License 2.0 5 votes vote down vote up
private <T extends Comparable<? super T>> T cast(String argument, Class<T> javaType) {
    if (javaType.isEnum()) {
        Class<? extends Enum> enumType = (Class<? extends Enum>) javaType;
        return (T) Enum.valueOf(enumType, argument);
    } else if (javaType == String.class) {
        return (T) argument;
    } else if (javaType == Integer.class || javaType == int.class) {
        return (T) Integer.valueOf(argument);
    } else if (javaType == Long.class || javaType == long.class) {
        return (T) Long.valueOf(argument);
    } else if (javaType == Boolean.class || javaType == boolean.class) {
        return (T) Boolean.valueOf(argument);
    } else if (javaType == Date.class) {
        try {
            DateTimeFormatter timeFormatter = DateTimeFormatter.ISO_DATE_TIME;
            OffsetDateTime offsetDateTime = OffsetDateTime.parse(argument, timeFormatter);
            return (T) Date.from(Instant.from(offsetDateTime));
        } catch (DateTimeParseException ex) {
            throw new RSQLException(
                    "The datetime must be in the ISO-8601 format with timezone, e.g. 1970-01-01T00:00:00Z, was "
                            + argument,
                    ex);
        }
    } else {
        throw new UnsupportedOperationException(
                "The target type " + javaType + " is not known to the type converter.");
    }
}
 
Example 9
Source File: TimeParser.java    From phoebus with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return a {@link TimeInterval} between this instant represented by the string and NOW
 * @param time
 * @return TimeInterval
 */
@Deprecated
public static TimeInterval getTimeInterval(String time) {
    Instant now = Instant.now();
    if (time.equalsIgnoreCase(NOW)) {
        return TimeInterval.between(now, now);
    } else {
        Matcher nUnitsAgoMatcher = timeQuantityUnitsPattern.matcher(time);
        while (nUnitsAgoMatcher.find()) {
            return TimeInterval.between(now.minus(parseTemporalAmount(nUnitsAgoMatcher.group(1))), now);
        }
        DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
        return TimeInterval.between(LocalDateTime.parse(time, formatter).atZone(TimeZone.getDefault().toZoneId()).toInstant(), now);
    }
}
 
Example 10
Source File: DateTimeFormatterFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new {@code DateTimeFormatter} using this factory.
 * <p>If no specific pattern or style has been defined,
 * the supplied {@code fallbackFormatter} will be used.
 * @param fallbackFormatter the fall-back formatter to use
 * when no specific factory properties have been set
 * @return a new date time formatter
 */
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
	DateTimeFormatter dateTimeFormatter = null;
	if (StringUtils.hasLength(this.pattern)) {
		// Using strict parsing to align with Joda-Time and standard DateFormat behavior:
		// otherwise, an overflow like e.g. Feb 29 for a non-leap-year wouldn't get rejected.
		// However, with strict parsing, a year digit needs to be specified as 'u'...
		String patternToUse = StringUtils.replace(this.pattern, "yy", "uu");
		dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);
	}
	else if (this.iso != null && this.iso != ISO.NONE) {
		switch (this.iso) {
			case DATE:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE;
				break;
			case TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_TIME;
				break;
			case DATE_TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
				break;
			default:
				throw new IllegalStateException("Unsupported ISO format: " + this.iso);
		}
	}
	else if (this.dateStyle != null && this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);
	}
	else if (this.dateStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(this.dateStyle);
	}
	else if (this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(this.timeStyle);
	}

	if (dateTimeFormatter != null && this.timeZone != null) {
		dateTimeFormatter = dateTimeFormatter.withZone(this.timeZone.toZoneId());
	}
	return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
 
Example 11
Source File: DateTimeFormatterFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new {@code DateTimeFormatter} using this factory.
 * <p>If no specific pattern or style has been defined,
 * the supplied {@code fallbackFormatter} will be used.
 * @param fallbackFormatter the fall-back formatter to use
 * when no specific factory properties have been set
 * @return a new date time formatter
 */
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
	DateTimeFormatter dateTimeFormatter = null;
	if (StringUtils.hasLength(this.pattern)) {
		// Using strict parsing to align with Joda-Time and standard DateFormat behavior:
		// otherwise, an overflow like e.g. Feb 29 for a non-leap-year wouldn't get rejected.
		// However, with strict parsing, a year digit needs to be specified as 'u'...
		String patternToUse = StringUtils.replace(this.pattern, "yy", "uu");
		dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);
	}
	else if (this.iso != null && this.iso != ISO.NONE) {
		switch (this.iso) {
			case DATE:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE;
				break;
			case TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_TIME;
				break;
			case DATE_TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
				break;
			default:
				throw new IllegalStateException("Unsupported ISO format: " + this.iso);
		}
	}
	else if (this.dateStyle != null && this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);
	}
	else if (this.dateStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(this.dateStyle);
	}
	else if (this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(this.timeStyle);
	}

	if (dateTimeFormatter != null && this.timeZone != null) {
		dateTimeFormatter = dateTimeFormatter.withZone(this.timeZone.toZoneId());
	}
	return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
 
Example 12
Source File: DateTimeFormater.java    From JavaSE8-Features with GNU General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {

        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter df = DateTimeFormatter.ISO_DATE;
        System.out.println(df.format(currentDate));

        LocalTime currentTime = LocalTime.now();
        DateTimeFormatter dt = DateTimeFormatter.ISO_TIME;
        System.out.println(dt.format(currentTime));

        LocalDateTime currentDT = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
        System.out.println(dtf.format(currentDT));

        DateTimeFormatter f_long = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        System.out.println(f_long.format(currentDT));

        DateTimeFormatter f_short = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
        System.out.println(f_short.format(currentDT));
        
        String fr_short = f_short.withLocale(Locale.FRENCH).format(currentDT);
        String fr_long = f_long.withLocale(Locale.FRENCH).format(currentDT);
        
        System.out.println(fr_short);
        System.out.println(fr_long);
        
        DateTimeFormatterBuilder b = new DateTimeFormatterBuilder()
                .appendValue(ChronoField.DAY_OF_YEAR)
                .appendLiteral("||")
                .appendValue(ChronoField.DAY_OF_MONTH)
                .appendLiteral("||")
                .appendValue(ChronoField.YEAR);
        
        DateTimeFormatter f = b.toFormatter();
        System.out.println(f.format(currentDT));
               

    }
 
Example 13
Source File: LogOutputSpec.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
public LogOutputSpecBuilder timeFormatterString(String formatOrConstant) {
    if (formatOrConstant == null || formatOrConstant.equalsIgnoreCase("NONE")
            || formatOrConstant.equalsIgnoreCase("FALSE")) {
        timeFormatter = null;
    } else if (formatOrConstant.length() == 0 || formatOrConstant.equalsIgnoreCase("DEFAULT")) {
        timeFormatter = DateTimeFormatter.ofPattern(DEFAULT_TIMESTAMP_PATTERN);
    } else if (formatOrConstant.equalsIgnoreCase("ISO8601")) {
        timeFormatter = DateTimeFormatter.ISO_DATE_TIME;
    } else if (formatOrConstant.equalsIgnoreCase("SHORT")) {
        timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
    } else if (formatOrConstant.equalsIgnoreCase("MEDIUM")) {
        timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
    } else if (formatOrConstant.equalsIgnoreCase("LONG")) {
        timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
    } else if (formatOrConstant.equalsIgnoreCase("FULL")) {
        timeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
    } else {
        try {
            timeFormatter = DateTimeFormatter.ofPattern(formatOrConstant);
        } catch (IllegalArgumentException exp) {
            throw new IllegalArgumentException(
                    "Cannot parse log date specification '" + formatOrConstant + "'." +
                            "Must be either DEFAULT, NONE, ISO8601, SHORT, MEDIUM, LONG, FULL or a " +
                            "format string parseable by DateTimeFormat. See " +
                            "https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html");
        }
    }
    return this;
}
 
Example 14
Source File: LocalDateTimeConverter.java    From sailfish-core with Apache License 2.0 4 votes vote down vote up
@Override
protected DateTimeFormatter getFormatter() {
    return DateTimeFormatter.ISO_DATE_TIME;
}
 
Example 15
Source File: ClusterRestApi.java    From submarine with Apache License 2.0 4 votes vote down vote up
private String formatLocalDateTime(LocalDateTime localDateTime) {
  DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
  String strDate = localDateTime.format(dtf);
  return strDate;
}
 
Example 16
Source File: JudgelsPlayUtils.java    From judgels with GNU General Public License v2.0 4 votes vote down vote up
public static String formatISOUTCDateTime(long timestamp) {
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.UTC);
    DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
    return formatter.format(zonedDateTime);
}
 
Example 17
Source File: DateTimeFormatterFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Create a new {@code DateTimeFormatter} using this factory.
 * <p>If no specific pattern or style has been defined,
 * the supplied {@code fallbackFormatter} will be used.
 * @param fallbackFormatter the fall-back formatter to use when no specific
 * factory properties have been set (can be {@code null}).
 * @return a new date time formatter
 */
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
	DateTimeFormatter dateTimeFormatter = null;
	if (StringUtils.hasLength(this.pattern)) {
		// Using strict parsing to align with Joda-Time and standard DateFormat behavior:
		// otherwise, an overflow like e.g. Feb 29 for a non-leap-year wouldn't get rejected.
		// However, with strict parsing, a year digit needs to be specified as 'u'...
		String patternToUse = this.pattern.replace("yy", "uu");
		dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);
	}
	else if (this.iso != null && this.iso != ISO.NONE) {
		switch (this.iso) {
			case DATE:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE;
				break;
			case TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_TIME;
				break;
			case DATE_TIME:
				dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
				break;
			case NONE:
				/* no-op */
				break;
			default:
				throw new IllegalStateException("Unsupported ISO format: " + this.iso);
		}
	}
	else if (this.dateStyle != null && this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);
	}
	else if (this.dateStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(this.dateStyle);
	}
	else if (this.timeStyle != null) {
		dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(this.timeStyle);
	}

	if (dateTimeFormatter != null && this.timeZone != null) {
		dateTimeFormatter = dateTimeFormatter.withZone(this.timeZone.toZoneId());
	}
	return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
 
Example 18
Source File: FmtZonedDateTimeTest.java    From super-csv with Apache License 2.0 4 votes vote down vote up
@Test
public void testConstructor4WithNullNext() {
	exception.expect(NullPointerException.class);
	new FmtZonedDateTime(DateTimeFormatter.ISO_DATE_TIME, null);
}
 
Example 19
Source File: ClusterRestApi.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
private String formatLocalDateTime(LocalDateTime localDateTime) {
  DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
  String strDate = localDateTime.format(dtf);
  return strDate;
}
 
Example 20
Source File: EventsFilter.java    From konker-platform with Apache License 2.0 3 votes vote down vote up
private Instant parseISODateTime(String text) {

        try {

            DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
            LocalDateTime datetime = LocalDateTime.parse(text, formatter);
            ZonedDateTime dt = datetime.atZone(ZoneId.of("UTC"));

            return dt.toInstant();

        } catch (DateTimeParseException e) {
            return null;
        }

    }