Java Instant Format formatInstant(TemporalAccessor instant)

Here you can find the source of formatInstant(TemporalAccessor instant)

Description

Formats the given instant to ISO8601 format including at least 3 positions for milliseconds.

License

Apache License

Parameter

Parameter Description
instant The instant to represent as ISO8601 data

Return

The ISO8601 representation of the instant in the UTC timezone.

Declaration

public static String formatInstant(TemporalAccessor instant) 

Method Source Code

//package com.java2s;
/*// w w  w.  j av a2s  . c  o m
 * Copyright (c) 2010-2017. Axon Framework
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.SignStyle;
import java.time.temporal.TemporalAccessor;
import static java.time.temporal.ChronoField.*;

public class Main {
    private static final DateTimeFormatter ISO_UTC_DATE_TIME = new DateTimeFormatterBuilder().parseCaseInsensitive()
            .appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD).appendLiteral('-').appendValue(MONTH_OF_YEAR, 2)
            .appendLiteral('-').appendValue(DAY_OF_MONTH, 2).appendLiteral('T').appendValue(HOUR_OF_DAY, 2)
            .appendLiteral(':').appendValue(MINUTE_OF_HOUR, 2).appendLiteral(':').appendValue(SECOND_OF_MINUTE, 2)
            .appendFraction(NANO_OF_SECOND, 3, 9, true).appendOffsetId().toFormatter().withZone(ZoneOffset.UTC);

    /**
     * Formats the given instant to ISO8601 format including at least 3 positions for milliseconds. This is to provide
     * for a String representation of an instant that is sortable by a textual sorting algorithm.
     *
     * @param instant The instant to represent as ISO8601 data
     * @return The ISO8601 representation of the instant in the UTC timezone.
     */
    public static String formatInstant(TemporalAccessor instant) {
        return ISO_UTC_DATE_TIME.format(instant);
    }
}

Related

  1. format(Instant instant)
  2. formatAtSAST(Instant instant, DateTimeFormatter format)
  3. formatDate(Instant date, Locale locale)
  4. formatDateForInstant(Instant instant)
  5. formatHttpDate(Instant instant)
  6. formatIso8601Date(Instant date)
  7. formatTimeLocal(Instant instant)
  8. parseInstant(String dateString, DateTimeFormatter formatter)
  9. toInfluxDBTimeFormat(final Instant time)