From: Marco Zanon Date: Thu, 15 Jun 2017 21:27:40 +0000 (+0000) Subject: Implemented MLocalDateConverter and MLocalDateTimeConverter. X-Git-Tag: 5.0.0~6 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=f3af1dea6aca3d3e470beecfb4257d9ffc50a4ab;p=Macaco Implemented MLocalDateConverter and MLocalDateTimeConverter. --- diff --git a/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java b/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java new file mode 100644 index 0000000..fa5e777 --- /dev/null +++ b/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java @@ -0,0 +1,134 @@ +/** + * Macaco + * Copyright (c) 2009-2017 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.conversion; + +import com.marcozanon.macaco.MObject; +import com.marcozanon.macaco.text.MText; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.Iterator; +import java.util.LinkedHashSet; + +public class MLocalDateConverter extends MObject { + + protected LinkedHashSet dateFormats = new LinkedHashSet(); + + /* */ + + public MLocalDateConverter(String defaultDateFormat) { + super(); + // + this.addDateFormat(defaultDateFormat); + } + + public MLocalDateConverter(LinkedHashSet dateFormats) { + super(); + // + this.setDateFormats(dateFormats); + } + + public MLocalDateConverter clone() { + return new MLocalDateConverter(this.getDateFormats()); + } + + /* Date formats. */ + + public void setDateFormats(LinkedHashSet dateFormats) { + if (null == dateFormats) { + throw new IllegalArgumentException("Invalid 'dateFormats': null."); + } + else { + Iterator i = dateFormats.iterator(); + while (i.hasNext()) { + MLocalDateConverter.checkDateFormat(i.next()); + } + } + // + synchronized (this.dateFormats) { + this.dateFormats = dateFormats; + } + } + + public void addDateFormat(String dateFormat) { + MLocalDateConverter.checkDateFormat(dateFormat); + // + synchronized (this.dateFormats) { + this.dateFormats.add(dateFormat); + } + } + + public LinkedHashSet getDateFormats() { + return this.dateFormats; + } + + public String getDefaultDateFormat() { + return this.getDateFormats().iterator().next(); + } + + /* Conversions. */ + + protected static void checkDateFormat(String dateFormat) { + if (MText.isBlank(dateFormat)) { + throw new IllegalArgumentException("Invalid 'dateFormat': null or empty."); + } + // + try { + DateTimeFormatter.ofPattern(dateFormat); + } + catch (IllegalArgumentException exception) { + throw new IllegalArgumentException(String.format("Invalid 'dateFormat': %s.", dateFormat)); // no need to propagate exception + } + } + + protected static LocalDate getDateFromStringByParameters(String x, String inputDateFormat) throws MInvalidConversionFormatException { + if (MText.isBlank(x)) { + throw new IllegalArgumentException("Invalid 'x': null or empty."); + } + MLocalDateConverter.checkDateFormat(inputDateFormat); + // + LocalDate d = null; + try { + d = LocalDate.parse(x, DateTimeFormatter.ofPattern(inputDateFormat)); + } + catch (DateTimeParseException exception) { + throw new MInvalidConversionFormatException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputDateFormat)); // no need to propagate exception + } + // + return d; + } + + protected static String getStringFromDateByParameters(LocalDate date, String outputDateFormat) { + if (null == date) { + throw new IllegalArgumentException("Invalid 'date': null."); + } + MLocalDateConverter.checkDateFormat(outputDateFormat); + // + return date.format(DateTimeFormatter.ofPattern(outputDateFormat)); + } + + public LocalDate getDateFromString(String x) throws MInvalidConversionFormatException { + LocalDate y = null; + for (String dateFormat: this.getDateFormats()) { + try { + y = MLocalDateConverter.getDateFromStringByParameters(x, dateFormat); + return y; + } + catch (MInvalidConversionFormatException exception) { + } + } + if (null == y) { + throw new MInvalidConversionFormatException(String.format("Invalid 'x': %s.", x)); + } + return y; // necessary to avoid Java compilation errors + } + + public String getStringFromDate(LocalDate x) { + return MLocalDateConverter.getStringFromDateByParameters(x, this.getDefaultDateFormat()); + } + +} diff --git a/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java b/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java new file mode 100644 index 0000000..956a7d2 --- /dev/null +++ b/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java @@ -0,0 +1,134 @@ +/** + * Macaco + * Copyright (c) 2009-2017 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.conversion; + +import com.marcozanon.macaco.MObject; +import com.marcozanon.macaco.text.MText; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.Iterator; +import java.util.LinkedHashSet; + +public class MLocalDateTimeConverter extends MObject { + + protected LinkedHashSet datetimeFormats = new LinkedHashSet(); + + /* */ + + public MLocalDateTimeConverter(String defaultDatetimeFormat) { + super(); + // + this.addDatetimeFormat(defaultDatetimeFormat); + } + + public MLocalDateTimeConverter(LinkedHashSet datetimeFormats) { + super(); + // + this.setDatetimeFormats(datetimeFormats); + } + + public MLocalDateTimeConverter clone() { + return new MLocalDateTimeConverter(this.getDatetimeFormats()); + } + + /* Datetime formats. */ + + public void setDatetimeFormats(LinkedHashSet datetimeFormats) { + if (null == datetimeFormats) { + throw new IllegalArgumentException("Invalid 'datetimeFormats': null."); + } + else { + Iterator i = datetimeFormats.iterator(); + while (i.hasNext()) { + MLocalDateTimeConverter.checkDatetimeFormat(i.next()); + } + } + // + synchronized (this.datetimeFormats) { + this.datetimeFormats = datetimeFormats; + } + } + + public void addDatetimeFormat(String datetimeFormat) { + MLocalDateTimeConverter.checkDatetimeFormat(datetimeFormat); + // + synchronized (this.datetimeFormats) { + this.datetimeFormats.add(datetimeFormat); + } + } + + public LinkedHashSet getDatetimeFormats() { + return this.datetimeFormats; + } + + public String getDefaultDatetimeFormat() { + return this.getDatetimeFormats().iterator().next(); + } + + /* Conversions. */ + + protected static void checkDatetimeFormat(String datetimeFormat) { + if (MText.isBlank(datetimeFormat)) { + throw new IllegalArgumentException("Invalid 'datetimeFormat': null or empty."); + } + // + try { + DateTimeFormatter.ofPattern(datetimeFormat); + } + catch (IllegalArgumentException exception) { + throw new IllegalArgumentException(String.format("Invalid 'datetimeFormat': %s.", datetimeFormat)); // no need to propagate exception + } + } + + protected static LocalDateTime getDatetimeFromStringByParameters(String x, String inputDatetimeFormat) throws MInvalidConversionFormatException { + if (MText.isBlank(x)) { + throw new IllegalArgumentException("Invalid 'x': null or empty."); + } + MLocalDateTimeConverter.checkDatetimeFormat(inputDatetimeFormat); + // + LocalDateTime dt = null; + try { + dt = LocalDateTime.parse(x, DateTimeFormatter.ofPattern(inputDatetimeFormat)); + } + catch (DateTimeParseException exception) { + throw new MInvalidConversionFormatException(String.format("Invalid 'x' or parsing: %s (input format: %s).", x, inputDatetimeFormat)); // no need to propagate exception + } + // + return dt; + } + + protected static String getStringFromDatetimeByParameters(LocalDateTime datetime, String outputDatetimeFormat) { + if (null == datetime) { + throw new IllegalArgumentException("Invalid 'datetime': null."); + } + MLocalDateTimeConverter.checkDatetimeFormat(outputDatetimeFormat); + // + return datetime.format(DateTimeFormatter.ofPattern(outputDatetimeFormat)); + } + + public LocalDateTime getDatetimeFromString(String x) throws MInvalidConversionFormatException { + LocalDateTime y = null; + for (String datetimeFormat: this.getDatetimeFormats()) { + try { + y = MLocalDateTimeConverter.getDatetimeFromStringByParameters(x, datetimeFormat); + return y; + } + catch (MInvalidConversionFormatException exception) { + } + } + if (null == y) { + throw new MInvalidConversionFormatException(String.format("Invalid 'x': %s.", x)); + } + return y; // necessary to avoid Java compilation errors + } + + public String getStringFromDatetime(LocalDateTime x) { + return MLocalDateTimeConverter.getStringFromDatetimeByParameters(x, this.getDefaultDatetimeFormat()); + } + +}