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;
|
|
|
|
|
2018-05-28 11:29:59 +02:00
|
|
|
import java.time.ZoneId;
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
2016-06-26 11:38:41 +02:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String format(Date date) {
|
2018-05-28 11:29:59 +02:00
|
|
|
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()).format(date.toInstant());
|
2016-06-26 11:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* added by msmock convert a long to ISO8601
|
2018-05-28 11:29:59 +02:00
|
|
|
*
|
2016-06-26 11:38:41 +02:00
|
|
|
* @param timePoint
|
2018-05-28 11:29:59 +02:00
|
|
|
* the timepoint
|
|
|
|
*
|
2016-06-26 11:38:41 +02:00
|
|
|
* @return time point as ISO8601 String
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String format(long timePoint) {
|
|
|
|
|
|
|
|
if (timePoint == Long.MAX_VALUE || timePoint == Long.MIN_VALUE) {
|
|
|
|
return "-";
|
|
|
|
}
|
|
|
|
|
|
|
|
// else
|
|
|
|
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
|
|
|
|
public long parseLong(String s) {
|
|
|
|
return parse(s).getTime();
|
|
|
|
}
|
|
|
|
|
2018-05-28 11:29:59 +02:00
|
|
|
public static void main(String[] args) {
|
|
|
|
Date d = new Date();
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
for (int i = 0; i < 10000000; i++) {
|
|
|
|
String s = new ISO8601().format(d);
|
|
|
|
Date d1 = new ISO8601().parse(s);
|
|
|
|
if (!d.equals(d1))
|
|
|
|
throw new IllegalStateException("Dates not same: " + d + " / " + d1);
|
|
|
|
}
|
|
|
|
System.out.println("Took " + (System.currentTimeMillis() - start));
|
|
|
|
}
|
|
|
|
|
2016-06-26 11:38:41 +02:00
|
|
|
/**
|
|
|
|
* parse ISO8601 date to long
|
2018-05-28 11:29:59 +02:00
|
|
|
*
|
2016-06-26 11:38:41 +02:00
|
|
|
* @param s
|
2018-05-28 11:29:59 +02:00
|
|
|
* the string to parse
|
|
|
|
*
|
2016-06-26 11:38:41 +02:00
|
|
|
* @return time point as long
|
2018-05-28 11:29:59 +02:00
|
|
|
*
|
2016-09-22 15:26:32 +02:00
|
|
|
* @throws IllegalArgumentException
|
2018-05-28 11:29:59 +02:00
|
|
|
* if the string can not be parsed
|
2016-06-26 11:38:41 +02:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public Date parse(String s) {
|
|
|
|
if (StringHelper.isEmpty(s)) {
|
|
|
|
String msg = "An empty value can not pe parsed to a date!";
|
|
|
|
throw new IllegalArgumentException(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.equals("-")) {
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.clear();
|
|
|
|
cal.setTimeZone(TimeZone.getTimeZone("GMT0"));
|
|
|
|
return cal.getTime();
|
|
|
|
}
|
|
|
|
|
2018-05-28 11:29:59 +02:00
|
|
|
ZonedDateTime zd = ZonedDateTime
|
|
|
|
.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
|
|
|
|
return Date.from(zd.toInstant());
|
2016-06-26 11:38:41 +02:00
|
|
|
}
|
|
|
|
}
|