Tagged: java8

format/parse ISO 8601 ignoring the fraction


    static Instant parseIsoInstant(final String formatted) {
        return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(formatted))
                .with(ChronoField.NANO_OF_SECOND, 0L);
    }

    static String formatIsoInstant(final TemporalAccessor parsed) {
        return DateTimeFormatter.ISO_INSTANT.format(
                Instant.from(parsed).with(ChronoField.NANO_OF_SECOND, 0L));
    }

    static Date parseIsoDate(final String formatted) {
        return Date.from(parseIsoInstant(formatted));
    }

    static String formatIsoDate(final Date parsed) {
        return formatIsoInstant(parsed.toInstant());
    }