[Minor] Directly use ISO8601 class for date parsing/formatting

This commit is contained in:
Robert von Burg 2018-10-15 10:23:19 +02:00
parent 1c6d873e5c
commit 23778a8ee0
3 changed files with 41 additions and 56 deletions

View File

@ -23,14 +23,14 @@ import java.util.Date;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.StrolchElementVisitor;
import li.strolch.utils.dbc.DBC;
import li.strolch.utils.iso8601.ISO8601FormatFactory;
import li.strolch.utils.iso8601.ISO8601;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class DateParameter extends AbstractParameter<Date> {
private static final Date EMPTY_VALUE = ISO8601FormatFactory.getInstance().getDateFormat().parse("-");
private static final Date EMPTY_VALUE = ISO8601.parseToDate("-");
private Date value;
@ -58,7 +58,7 @@ public class DateParameter extends AbstractParameter<Date> {
@Override
public String getValueAsString() {
return ISO8601FormatFactory.getInstance().formatDate(this.value);
return ISO8601.toString(this.value);
}
@SuppressWarnings("unchecked")
@ -162,7 +162,7 @@ public class DateParameter extends AbstractParameter<Date> {
}
public static Date parseFromString(String valueS) {
return ISO8601FormatFactory.getInstance().getDateFormat().parse(valueS);
return ISO8601.parseToDate(valueS);
}
@Override

View File

@ -32,6 +32,7 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
private PostgreSqlOrderDao orderDao;
private PostgreSqlResourceDao resourceDao;
private PostgreSqlActivityDao activityDao;
private PostgreSqlLogMessageDao logMessageDao;
private AuditDao auditDao;
private Connection connection;
@ -99,12 +100,18 @@ public class PostgreSqlStrolchTransaction extends AbstractTransaction {
return this.activityDao;
}
public AuditDao getAuditDao() {
AuditDao getAuditDao() {
if (this.auditDao == null)
this.auditDao = new PostgreSqlAuditDao(this);
return this.auditDao;
}
LogMessageDao getLogMessageDao() {
if (this.logMessageDao == null)
this.logMessageDao = new PostgreSqlLogMessageDao(this);
return this.logMessageDao;
}
Connection getConnection() {
if (this.connection == null)
this.connection = this.persistenceHandler.getConnection(getRealm().getRealm());

View File

@ -16,11 +16,10 @@
package li.strolch.utils.iso8601;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import li.strolch.utils.helper.StringHelper;
import org.slf4j.Logger;
@ -34,27 +33,11 @@ public class ISO8601 implements DateFormat {
private static final Logger logger = LoggerFactory.getLogger(ISO8601.class);
@Override
public String format(Date date) {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()).format(date.toInstant());
}
/**
* added by msmock convert a long to ISO8601
*
* @param timePoint
* the timepoint
*
* @return time point as ISO8601 String
*/
@Override
public String format(long timePoint) {
if (timePoint == Long.MAX_VALUE || timePoint == Long.MIN_VALUE) {
if (timePoint == Long.MAX_VALUE || timePoint == Long.MIN_VALUE)
return "-";
}
// else
try {
Date date = new Date();
date.setTime(timePoint);
@ -65,50 +48,45 @@ public class ISO8601 implements DateFormat {
}
}
@Override
public String format(Date date) {
return toString(date);
}
@Override
public long parseLong(String s) {
return parse(s).getTime();
}
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));
}
/**
* parse ISO8601 date to long
*
* @param s
* the string to parse
*
* @return time point as long
*
* @throws IllegalArgumentException
* if the string can not be parsed
*/
@Override
public Date parse(String s) {
return parseToDate(s);
}
public static ZonedDateTime parseToZdt(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();
}
if (s.equals("-"))
return ZonedDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
return ZonedDateTime.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
}
ZonedDateTime zd = ZonedDateTime
.parse(s, DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()));
return Date.from(zd.toInstant());
public static Date parseToDate(String s) {
return Date.from(parseToZdt(s).toInstant());
}
public static String toString(ZonedDateTime zonedDateTime) {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()).format(zonedDateTime);
}
public static String toString(Date date) {
return DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.systemDefault()).format(date.toInstant());
}
public static void main(String[] args) {
System.out.println(toString(ISO8601.parseToZdt("-")));
}
}