if (null == dateFormats) {
throw new IllegalArgumentException("Invalid 'dateFormats': null.");
}
+ else if (0 == dateFormats.size()) {
+ throw new IllegalArgumentException("Invalid 'dateFormats': empty.");
+ }
else {
- Iterator<String> i = dateFormats.iterator();
- while (i.hasNext()) {
- MDateConverter.checkDateFormat(i.next());
+ for (String dateFormat: dateFormats) {
+ MDateConverter.checkDateFormat(dateFormat);
}
}
//
import java.time.format.DateTimeParseException;
import java.util.Iterator;
import java.util.LinkedHashSet;
+import java.util.Locale;
public class MLocalDateConverter extends MObject {
protected LinkedHashSet<String> dateFormats = new LinkedHashSet<String>();
+ protected Locale locale = null;
/* */
- public MLocalDateConverter(String defaultDateFormat) {
+ public MLocalDateConverter(String defaultDateFormat, Locale locale) {
super();
//
this.addDateFormat(defaultDateFormat);
+ this.setLocale(locale);
}
- public MLocalDateConverter(LinkedHashSet<String> dateFormats) {
+ public MLocalDateConverter(LinkedHashSet<String> dateFormats, Locale locale) {
super();
//
this.setDateFormats(dateFormats);
+ this.setLocale(locale);
}
public MLocalDateConverter clone() {
- return new MLocalDateConverter(this.getDateFormats());
+ return new MLocalDateConverter(this.getDateFormats(), this.getLocale());
}
/* Date formats. */
if (null == dateFormats) {
throw new IllegalArgumentException("Invalid 'dateFormats': null.");
}
+ else if (0 == dateFormats.size()) {
+ throw new IllegalArgumentException("Invalid 'dateFormats': empty.");
+ }
else {
- Iterator<String> i = dateFormats.iterator();
- while (i.hasNext()) {
- MLocalDateConverter.checkDateFormat(i.next());
+ for (String dateFormat: dateFormats) {
+ MLocalDateConverter.checkDateFormat(dateFormat);
}
}
//
return this.getDateFormats().iterator().next();
}
+ /* Locale. */
+
+ protected void setLocale(Locale locale) {
+ if (null == locale) {
+ throw new IllegalArgumentException("Invalid 'locale': null.");
+ }
+ //
+ this.locale = locale;
+ }
+
+ public Locale getLocale() {
+ return this.locale;
+ }
+
/* Conversions. */
protected static void checkDateFormat(String dateFormat) {
}
}
- protected static LocalDate getDateFromStringByParameters(String x, String inputDateFormat) throws MInvalidConversionFormatException {
+ protected static LocalDate getDateFromStringByParameters(String x, String inputDateFormat, Locale inputLocale) throws MInvalidConversionFormatException {
if (MText.isBlank(x)) {
throw new IllegalArgumentException("Invalid 'x': null or empty.");
}
MLocalDateConverter.checkDateFormat(inputDateFormat);
+ if (null == inputLocale) {
+ throw new IllegalArgumentException("Invalid 'inputLocale': null.");
+ }
//
LocalDate d = null;
try {
- d = LocalDate.parse(x, DateTimeFormatter.ofPattern(inputDateFormat));
+ d = LocalDate.parse(x, DateTimeFormatter.ofPattern(inputDateFormat, inputLocale));
}
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) {
+ protected static String getStringFromDateByParameters(LocalDate date, String outputDateFormat, Locale outputLocale) {
if (null == date) {
throw new IllegalArgumentException("Invalid 'date': null.");
}
MLocalDateConverter.checkDateFormat(outputDateFormat);
+ if (null == outputLocale) {
+ throw new IllegalArgumentException("Invalid 'outputLocale': null.");
+ }
//
- return date.format(DateTimeFormatter.ofPattern(outputDateFormat));
+ return date.format(DateTimeFormatter.ofPattern(outputDateFormat, outputLocale));
}
public LocalDate getDateFromString(String x) throws MInvalidConversionFormatException {
LocalDate y = null;
for (String dateFormat: this.getDateFormats()) {
try {
- y = MLocalDateConverter.getDateFromStringByParameters(x, dateFormat);
+ y = MLocalDateConverter.getDateFromStringByParameters(x, dateFormat, this.getLocale());
return y;
}
catch (MInvalidConversionFormatException exception) {
}
public String getStringFromDate(LocalDate x) {
- return MLocalDateConverter.getStringFromDateByParameters(x, this.getDefaultDateFormat());
+ return MLocalDateConverter.getStringFromDateByParameters(x, this.getDefaultDateFormat(), this.getLocale());
}
}
import java.time.format.DateTimeParseException;
import java.util.Iterator;
import java.util.LinkedHashSet;
+import java.util.Locale;
public class MLocalDateTimeConverter extends MObject {
protected LinkedHashSet<String> datetimeFormats = new LinkedHashSet<String>();
+ protected Locale locale = null;
/* */
- public MLocalDateTimeConverter(String defaultDatetimeFormat) {
+ public MLocalDateTimeConverter(String defaultDatetimeFormat, Locale locale) {
super();
//
this.addDatetimeFormat(defaultDatetimeFormat);
+ this.setLocale(locale);
}
- public MLocalDateTimeConverter(LinkedHashSet<String> datetimeFormats) {
+ public MLocalDateTimeConverter(LinkedHashSet<String> datetimeFormats, Locale locale) {
super();
//
this.setDatetimeFormats(datetimeFormats);
+ this.setLocale(locale);
}
public MLocalDateTimeConverter clone() {
- return new MLocalDateTimeConverter(this.getDatetimeFormats());
+ return new MLocalDateTimeConverter(this.getDatetimeFormats(), this.getLocale());
}
/* Datetime formats. */
if (null == datetimeFormats) {
throw new IllegalArgumentException("Invalid 'datetimeFormats': null.");
}
+ else if (0 == datetimeFormats.size()) {
+ throw new IllegalArgumentException("Invalid 'datetimeFormats': empty.");
+ }
else {
- Iterator<String> i = datetimeFormats.iterator();
- while (i.hasNext()) {
- MLocalDateTimeConverter.checkDatetimeFormat(i.next());
+ for (String datetimeFormat: datetimeFormats) {
+ MLocalDateTimeConverter.checkDatetimeFormat(datetimeFormat);
}
}
//
return this.getDatetimeFormats().iterator().next();
}
+ /* Locale. */
+
+ protected void setLocale(Locale locale) {
+ if (null == locale) {
+ throw new IllegalArgumentException("Invalid 'locale': null.");
+ }
+ //
+ this.locale = locale;
+ }
+
+ public Locale getLocale() {
+ return this.locale;
+ }
+
/* Conversions. */
protected static void checkDatetimeFormat(String datetimeFormat) {
}
}
- protected static LocalDateTime getDatetimeFromStringByParameters(String x, String inputDatetimeFormat) throws MInvalidConversionFormatException {
+ protected static LocalDateTime getDatetimeFromStringByParameters(String x, String inputDatetimeFormat, Locale inputLocale) throws MInvalidConversionFormatException {
if (MText.isBlank(x)) {
throw new IllegalArgumentException("Invalid 'x': null or empty.");
}
MLocalDateTimeConverter.checkDatetimeFormat(inputDatetimeFormat);
+ if (null == inputLocale) {
+ throw new IllegalArgumentException("Invalid 'inputLocale': null.");
+ }
//
LocalDateTime dt = null;
try {
- dt = LocalDateTime.parse(x, DateTimeFormatter.ofPattern(inputDatetimeFormat));
+ dt = LocalDateTime.parse(x, DateTimeFormatter.ofPattern(inputDatetimeFormat, inputLocale));
}
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) {
+ protected static String getStringFromDatetimeByParameters(LocalDateTime datetime, String outputDatetimeFormat, Locale outputLocale) {
if (null == datetime) {
throw new IllegalArgumentException("Invalid 'datetime': null.");
}
MLocalDateTimeConverter.checkDatetimeFormat(outputDatetimeFormat);
+ if (null == outputLocale) {
+ throw new IllegalArgumentException("Invalid 'outputLocale': null.");
+ }
//
- return datetime.format(DateTimeFormatter.ofPattern(outputDatetimeFormat));
+ return datetime.format(DateTimeFormatter.ofPattern(outputDatetimeFormat, outputLocale));
}
public LocalDateTime getDatetimeFromString(String x) throws MInvalidConversionFormatException {
LocalDateTime y = null;
for (String datetimeFormat: this.getDatetimeFormats()) {
try {
- y = MLocalDateTimeConverter.getDatetimeFromStringByParameters(x, datetimeFormat);
+ y = MLocalDateTimeConverter.getDatetimeFromStringByParameters(x, datetimeFormat, this.getLocale());
return y;
}
catch (MInvalidConversionFormatException exception) {
}
public String getStringFromDatetime(LocalDateTime x) {
- return MLocalDateTimeConverter.getStringFromDatetimeByParameters(x, this.getDefaultDatetimeFormat());
+ return MLocalDateTimeConverter.getStringFromDatetimeByParameters(x, this.getDefaultDatetimeFormat(), this.getLocale());
}
}
if (null == numberFormats) {
throw new IllegalArgumentException("Invalid 'numberFormats': null.");
}
+ else if (0 == numberFormats.size()) {
+ throw new IllegalArgumentException("Invalid 'numberFormats': empty.");
+ }
else {
- Iterator<String> i = numberFormats.iterator();
- while (i.hasNext()) {
- MNumberConverter.checkNumberFormat(i.next());
+ for (String numberFormat: numberFormats) {
+ MNumberConverter.checkNumberFormat(numberFormat);
}
}
//