[New] Extended DateRange to use new Java 8+ date classes

This commit is contained in:
Robert von Burg 2020-03-26 14:35:51 +01:00
parent d9652d61d9
commit 14f994bd83
1 changed files with 87 additions and 16 deletions

View File

@ -15,10 +15,14 @@
*/ */
package li.strolch.utils.collections; package li.strolch.utils.collections;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date; import java.util.Date;
import li.strolch.utils.dbc.DBC; 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> * @author Robert von Burg <eitch@eitchnet.ch>
@ -27,42 +31,102 @@ public class DateRange {
private boolean fromInclusive; private boolean fromInclusive;
private boolean toInclusive; private boolean toInclusive;
private Date fromDate; private ZonedDateTime fromDate;
private Date toDate; private ZonedDateTime toDate;
public DateRange from(Date from, boolean inclusive) { public DateRange from(LocalDate from, boolean inclusive) {
this.fromDate = ZonedDateTime.of(from.atStartOfDay(), ZoneId.systemDefault());
this.fromInclusive = inclusive;
validate();
return this;
}
public DateRange to(LocalDate to, boolean inclusive) {
this.toDate = ZonedDateTime.of(to.atStartOfDay(), ZoneId.systemDefault());
this.toInclusive = inclusive;
validate();
return this;
}
public DateRange from(LocalDateTime from, boolean inclusive) {
this.fromDate = ZonedDateTime.of(from, ZoneId.systemDefault());
this.fromInclusive = inclusive;
validate();
return this;
}
public DateRange to(LocalDateTime to, boolean inclusive) {
this.toDate = ZonedDateTime.of(to, ZoneId.systemDefault());
this.toInclusive = inclusive;
validate();
return this;
}
public DateRange from(ZonedDateTime from, boolean inclusive) {
this.fromDate = from; this.fromDate = from;
this.fromInclusive = inclusive; this.fromInclusive = inclusive;
validate(); validate();
return this; return this;
} }
public DateRange to(Date to, boolean inclusive) { public DateRange to(ZonedDateTime to, boolean inclusive) {
this.toDate = to; this.toDate = to;
this.toInclusive = inclusive; this.toInclusive = inclusive;
validate(); validate();
return this; return this;
} }
public DateRange from(Date from, boolean inclusive) {
this.fromDate = ZonedDateTime.ofInstant(from.toInstant(), ZoneId.systemDefault());
this.fromInclusive = inclusive;
validate();
return this;
}
public DateRange to(Date to, boolean inclusive) {
this.toDate = ZonedDateTime.ofInstant(to.toInstant(), ZoneId.systemDefault());
this.toInclusive = inclusive;
validate();
return this;
}
private void validate() { private void validate() {
if (this.toDate != null && this.fromDate != null) if (this.toDate != null && this.fromDate != null)
DBC.INTERIM.assertTrue("From must be before to!", this.toDate.compareTo(this.fromDate) >= 0); //$NON-NLS-1$ DBC.INTERIM.assertTrue("From must be before to!", this.toDate.compareTo(this.fromDate) >= 0); //$NON-NLS-1$
} }
/** public boolean isFromInclusive() {
* @return from date return this.fromInclusive;
*/ }
public Date getFromDate() {
public boolean isToInclusive() {
return this.toInclusive;
}
public ZonedDateTime getFromDateZdt() {
return this.fromDate; return this.fromDate;
} }
/** public ZonedDateTime getToDateZdt() {
* @return to date
*/
public Date getToDate() {
return this.toDate; return this.toDate;
} }
public LocalDateTime getFromDateLdt() {
return this.fromDate.toLocalDateTime();
}
public LocalDateTime getToDateLdt() {
return this.toDate.toLocalDateTime();
}
public Date getFromDate() {
return Date.from(this.fromDate.toInstant());
}
public Date getToDate() {
return Date.from(this.toDate.toInstant());
}
/** /**
* @return true if from is set * @return true if from is set
*/ */
@ -99,6 +163,14 @@ public class DateRange {
} }
public boolean contains(Date date) { public boolean contains(Date date) {
return contains(ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
}
public boolean contains(LocalDateTime date) {
return contains(ZonedDateTime.of(date, ZoneId.systemDefault()));
}
public boolean contains(ZonedDateTime date) {
DBC.PRE.assertNotNull("Date must be given!", date); //$NON-NLS-1$ DBC.PRE.assertNotNull("Date must be given!", date); //$NON-NLS-1$
if (this.fromDate == null && this.toDate == null) if (this.fromDate == null && this.toDate == null)
return true; return true;
@ -126,12 +198,11 @@ public class DateRange {
@Override @Override
public String toString() { public String toString() {
ISO8601FormatFactory df = ISO8601FormatFactory.getInstance();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(this.fromDate == null ? "-" : df.formatDate(this.fromDate)); sb.append(this.fromDate == null ? "-" : ISO8601.toString(this.fromDate));
sb.append((this.fromInclusive ? " (inc)" : " (exc)")); sb.append((this.fromInclusive ? " (inc)" : " (exc)"));
sb.append(" - "); sb.append(" - ");
sb.append(this.toDate == null ? "-" : df.formatDate(this.toDate)); sb.append(this.toDate == null ? "-" : ISO8601.toString(this.toDate));
sb.append((this.toInclusive ? " (inc)" : " (exc)")); sb.append((this.toInclusive ? " (inc)" : " (exc)"));
return sb.toString(); return sb.toString();
} }