Implemented MLocalDateConverter and MLocalDateTimeConverter.
authorMarco Zanon <info@marcozanon.com>
Thu, 15 Jun 2017 21:27:40 +0000 (21:27 +0000)
committerMarco Zanon <info@marcozanon.com>
Thu, 15 Jun 2017 21:27:40 +0000 (21:27 +0000)
5.x/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java [new file with mode: 0644]
5.x/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java [new file with mode: 0644]

diff --git a/5.x/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java b/5.x/src/java/com/marcozanon/macaco/conversion/MLocalDateConverter.java
new file mode 100644 (file)
index 0000000..fa5e777
--- /dev/null
@@ -0,0 +1,134 @@
+/**
+ * Macaco
+ * Copyright (c) 2009-2017 Marco Zanon <info@marcozanon.com>.
+ * 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<String> dateFormats = new LinkedHashSet<String>();
+
+    /* */
+
+    public MLocalDateConverter(String defaultDateFormat) {
+        super();
+        //
+        this.addDateFormat(defaultDateFormat);
+    }
+
+    public MLocalDateConverter(LinkedHashSet<String> dateFormats) {
+        super();
+        //
+        this.setDateFormats(dateFormats);
+    }
+
+    public MLocalDateConverter clone() {
+        return new MLocalDateConverter(this.getDateFormats());
+    }
+
+    /* Date formats. */
+
+    public void setDateFormats(LinkedHashSet<String> dateFormats) {
+        if (null == dateFormats) {
+            throw new IllegalArgumentException("Invalid 'dateFormats': null.");
+        }
+        else {
+            Iterator<String> 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<String> 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/5.x/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java b/5.x/src/java/com/marcozanon/macaco/conversion/MLocalDateTimeConverter.java
new file mode 100644 (file)
index 0000000..956a7d2
--- /dev/null
@@ -0,0 +1,134 @@
+/**
+ * Macaco
+ * Copyright (c) 2009-2017 Marco Zanon <info@marcozanon.com>.
+ * 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<String> datetimeFormats = new LinkedHashSet<String>();
+
+    /* */
+
+    public MLocalDateTimeConverter(String defaultDatetimeFormat) {
+        super();
+        //
+        this.addDatetimeFormat(defaultDatetimeFormat);
+    }
+
+    public MLocalDateTimeConverter(LinkedHashSet<String> datetimeFormats) {
+        super();
+        //
+        this.setDatetimeFormats(datetimeFormats);
+    }
+
+    public MLocalDateTimeConverter clone() {
+        return new MLocalDateTimeConverter(this.getDatetimeFormats());
+    }
+
+    /* Datetime formats. */
+
+    public void setDatetimeFormats(LinkedHashSet<String> datetimeFormats) {
+        if (null == datetimeFormats) {
+            throw new IllegalArgumentException("Invalid 'datetimeFormats': null.");
+        }
+        else {
+            Iterator<String> 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<String> 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());
+    }
+
+}