2016-06-26 11:38:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Martin Smock <smock.martin@gmail.com>
|
2018-05-28 11:29:59 +02:00
|
|
|
*
|
2016-06-26 11:38:41 +02:00
|
|
|
* 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
|
2018-05-28 11:29:59 +02:00
|
|
|
*
|
2016-06-26 11:38:41 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-05-28 11:29:59 +02:00
|
|
|
*
|
2016-06-26 11:38:41 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
package li.strolch.utils.iso8601;
|
|
|
|
|
2020-06-09 23:18:28 +02:00
|
|
|
import static java.time.ZoneId.systemDefault;
|
|
|
|
import static java.time.temporal.ChronoField.*;
|
|
|
|
|
2019-10-23 10:01:03 +02:00
|
|
|
import java.time.LocalDateTime;
|
2018-05-28 11:29:59 +02:00
|
|
|
import java.time.ZoneId;
|
2018-10-15 10:23:19 +02:00
|
|
|
import java.time.ZoneOffset;
|
2018-05-28 11:29:59 +02:00
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
2020-06-09 23:18:28 +02:00
|
|
|
import java.time.format.DateTimeFormatterBuilder;
|
|
|
|
import java.time.format.SignStyle;
|
2016-06-26 11:38:41 +02:00
|
|
|
import java.util.Date;
|
|
|
|
|
2018-05-28 11:29:59 +02:00
|
|
|
import li.strolch.utils.helper.StringHelper;
|
2016-06-26 11:38:41 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2016-09-22 15:26:32 +02:00
|
|
|
* @author Martin Smock <smock.martin@gmail.com>
|
2016-06-26 11:38:41 +02:00
|
|
|
*/
|
|
|
|
@SuppressWarnings("nls")
|
|
|
|
public class ISO8601 implements DateFormat {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ISO8601.class);
|
|
|
|
|
2019-03-27 20:56:18 +01:00
|
|
|
public static final Date EMPTY_VALUE = parseToDate("-");
|
2019-07-03 20:36:37 +02:00
|
|
|
public static final ZonedDateTime EMPTY_VALUE_ZONED_DATE = ZonedDateTime
|
2020-06-09 23:18:28 +02:00
|
|
|
.ofInstant(EMPTY_VALUE.toInstant(), systemDefault());
|
|
|
|
|
|
|
|
public static final DateTimeFormatter ISO_LOCAL_DATE;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ISO_LOCAL_DATE = new DateTimeFormatterBuilder().appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD) //
|
|
|
|
.appendLiteral('-') //
|
|
|
|
.appendValue(MONTH_OF_YEAR, 2) //
|
|
|
|
.appendLiteral('-') //
|
|
|
|
.appendValue(DAY_OF_MONTH, 2) //
|
|
|
|
.toFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final DateTimeFormatter ISO_LOCAL_TIME;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ISO_LOCAL_TIME = new DateTimeFormatterBuilder() //
|
|
|
|
.appendValue(HOUR_OF_DAY, 2) //
|
|
|
|
.appendLiteral(':') //
|
|
|
|
.appendValue(MINUTE_OF_HOUR, 2) //
|
|
|
|
.optionalStart() //
|
|
|
|
.appendLiteral(':') //
|
|
|
|
.appendValue(SECOND_OF_MINUTE, 2) //
|
|
|
|
.optionalStart() //
|
|
|
|
.appendFraction(NANO_OF_SECOND, 0, 3, true) //
|
|
|
|
.toFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final DateTimeFormatter ISO_LOCAL_TIME_NO_FRACTIONS;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ISO_LOCAL_TIME_NO_FRACTIONS = new DateTimeFormatterBuilder() //
|
|
|
|
.appendValue(HOUR_OF_DAY, 2) //
|
|
|
|
.appendLiteral(':') //
|
|
|
|
.appendValue(MINUTE_OF_HOUR, 2) //
|
|
|
|
.optionalStart() //
|
|
|
|
.appendLiteral(':') //
|
|
|
|
.appendValue(SECOND_OF_MINUTE, 2) //
|
|
|
|
.toFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final DateTimeFormatter ISO_LOCAL_DATE_TIME;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ISO_LOCAL_DATE_TIME = new DateTimeFormatterBuilder() //
|
|
|
|
.parseCaseInsensitive() //
|
|
|
|
.append(ISO_LOCAL_DATE) //
|
|
|
|
.appendLiteral('T') //
|
|
|
|
.append(ISO_LOCAL_TIME) //
|
|
|
|
.toFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final DateTimeFormatter ISO_LOCAL_DATE_TIME_NO_FRACTIONS;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ISO_LOCAL_DATE_TIME_NO_FRACTIONS = new DateTimeFormatterBuilder() //
|
|
|
|
.parseCaseInsensitive() //
|
|
|
|
.append(ISO_LOCAL_DATE) //
|
|
|
|
.appendLiteral('T') //
|
|
|
|
.append(ISO_LOCAL_TIME_NO_FRACTIONS) //
|
|
|
|
.toFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final DateTimeFormatter ISO_OFFSET_DATE_TIME;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ISO_OFFSET_DATE_TIME = new DateTimeFormatterBuilder() //
|
|
|
|
.parseCaseInsensitive() //
|
|
|
|
.append(ISO_LOCAL_DATE_TIME) //
|
|
|
|
.parseLenient() //
|
|
|
|
.appendOffsetId() //
|
|
|
|
.parseStrict() //
|
|
|
|
.toFormatter();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final DateTimeFormatter ISO_OFFSET_DATE_TIME_ZONE;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ISO_OFFSET_DATE_TIME_ZONE = ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final DateTimeFormatter ISO_OFFSET_DATE_TIME_ZONE_NO_FRACTIONS;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ISO_OFFSET_DATE_TIME_ZONE_NO_FRACTIONS = new DateTimeFormatterBuilder() //
|
|
|
|
.parseCaseInsensitive() //
|
|
|
|
.append(ISO_LOCAL_DATE_TIME_NO_FRACTIONS) //
|
|
|
|
.parseLenient() //
|
|
|
|
.appendOffsetId() //
|
|
|
|
.parseStrict() //
|
|
|
|
.toFormatter();
|
|
|
|
}
|
2019-03-27 20:56:18 +01:00
|
|
|
|
2016-06-26 11:38:41 +02:00
|
|
|
@Override
|
|
|
|
public String format(long timePoint) {
|
2018-10-15 10:23:19 +02:00
|
|
|
if (timePoint == Long.MAX_VALUE || timePoint == Long.MIN_VALUE)
|
2016-06-26 11:38:41 +02:00
|
|
|
return "-";
|
|
|
|
|
|
|
|
try {
|
|
|
|
Date date = new Date();
|
|
|
|
date.setTime(timePoint);
|
2018-05-28 11:29:59 +02:00
|
|
|
return format(date);
|
2016-06-26 11:38:41 +02:00
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-10-15 10:23:19 +02:00
|
|
|
public String format(Date date) {
|
|
|
|
return toString(date);
|
2016-06-26 11:38:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 10:23:19 +02:00
|
|
|
@Override
|
|
|
|
public long parseLong(String s) {
|
|
|
|
return parse(s).getTime();
|
2018-05-28 11:29:59 +02:00
|
|
|
}
|
|
|
|
|
2016-06-26 11:38:41 +02:00
|
|
|
@Override
|
|
|
|
public Date parse(String s) {
|
2018-10-15 10:23:19 +02:00
|
|
|
return parseToDate(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ZonedDateTime parseToZdt(String s) {
|
2016-06-26 11:38:41 +02:00
|
|
|
if (StringHelper.isEmpty(s)) {
|
|
|
|
String msg = "An empty value can not pe parsed to a date!";
|
|
|
|
throw new IllegalArgumentException(msg);
|
|
|
|
}
|
|
|
|
|
2018-10-15 10:23:19 +02:00
|
|
|
if (s.equals("-"))
|
|
|
|
return ZonedDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
|
2020-06-09 23:18:28 +02:00
|
|
|
return ZonedDateTime.parse(s, getIso8601Formatter(false));
|
2018-10-15 10:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Date parseToDate(String s) {
|
|
|
|
return Date.from(parseToZdt(s).toInstant());
|
|
|
|
}
|
2016-06-26 11:38:41 +02:00
|
|
|
|
2020-06-09 23:18:28 +02:00
|
|
|
public static String toString(LocalDateTime localDateTime) {
|
|
|
|
return toString(localDateTime.atZone(systemDefault()));
|
2018-10-15 10:23:19 +02:00
|
|
|
}
|
|
|
|
|
2020-06-09 23:18:28 +02:00
|
|
|
public static String toString(ZonedDateTime zonedDateTime) {
|
|
|
|
return getIso8601Formatter(false).format(zonedDateTime);
|
2019-10-23 10:01:03 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 10:23:19 +02:00
|
|
|
public static String toString(Date date) {
|
2020-06-09 23:18:28 +02:00
|
|
|
return getIso8601Formatter(false).format(date.toInstant());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static DateTimeFormatter getIso8601Formatter(boolean noFractions) {
|
|
|
|
if (noFractions)
|
|
|
|
return ISO_OFFSET_DATE_TIME_ZONE_NO_FRACTIONS;
|
|
|
|
return ISO_OFFSET_DATE_TIME_ZONE;
|
2018-10-15 10:23:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
System.out.println(toString(ISO8601.parseToZdt("-")));
|
2019-10-23 10:01:03 +02:00
|
|
|
System.out.println(toString(ZonedDateTime.now()));
|
|
|
|
System.out.println(toString(LocalDateTime.now()));
|
2016-06-26 11:38:41 +02:00
|
|
|
}
|
|
|
|
}
|