Returns the ISO8601 string of a given date-time instance with timezone information, trying to be as closed as possible to what the JODA date-time library would return. - Java java.time

Java examples for java.time:Timezone

Description

Returns the ISO8601 string of a given date-time instance with timezone information, trying to be as closed as possible to what the JODA date-time library would return.

Demo Code

/*/*from w ww .  ja  v a  2  s .  c o  m*/
 * ModeShape (http://www.modeshape.org)
 *
 * 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.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.SignStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;

public class Main{
    /**
     * ISO 8601 formatter which attempts to be as close to the previous behavior (JODA) as possible. JDK 8 has some
     * significant differences especially when it comes to milliseconds, which it doesn't support out-of-the-box. 
     * 
     * However, because of this bug in JDK 8: 
     * 
     * - https://bugs.openjdk.java.net/browse/JDK-8031085 this expression
     * - http://bugs.java.com/view_bug.do?bug_id=8032491
     * 
     * is WAY MORE COMPLICATED than it should be (in reality is should use the .SSS pattern)
     */
    private static DateTimeFormatter JODA_ISO8601_FORMATTER = new DateTimeFormatterBuilder()
            .parseLenient()
            .appendPattern("uuuu-MM-dd['T'HH:mm:ss][.")
            .appendValue(ChronoField.MILLI_OF_SECOND, 3, 3, SignStyle.NEVER)
            .optionalEnd().appendPattern("[XXXXX]").toFormatter();
    /**
     * Returns the ISO8601 string of a given date-time instance with timezone information, trying to be as closed as possible
     * to what the JODA date-time library would return.
     * 
     * @param dateTime a {@link ZonedDateTime} instance, may not be null
     * @return a {@link String} representation of the date instance according to the ISO8601 standard
     */
    public static String jodaFormat(ZonedDateTime dateTime) {
        CheckArg.isNotNull(dateTime, "dateTime");
        return dateTime.format(JODA_ISO8601_FORMATTER);
    }
}

Related Tutorials