Fixing date formatting

This commit is contained in:
Francesco Mangiacrapa 2022-09-22 15:49:47 +02:00
parent 8d64e2972c
commit e4d1ae1a28
1 changed files with 36 additions and 19 deletions

View File

@ -74,7 +74,7 @@ public class ConvertToDataValueObjectModel {
private static final String NO_TIME = "T00:00"; private static final String NO_TIME = "T00:00";
public static final String DATE_FORMAT = "dd-MM-yyyy"; public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String HOURS_MINUTES_SEPARATOR = ":"; public static final String HOURS_MINUTES_SEPARATOR = ":";
@ -709,26 +709,41 @@ public class ConvertToDataValueObjectModel {
return null; return null;
String time = dateTime.toString(); String time = dateTime.toString();
return instantToDateFormatString(time);
}
/**
* Instant to date format string. format dd-MM-yyyyT00:00:00.00Z to
* DateFormat.format
*
* @param instantString the instant string
* @return the string
*/
public static String instantToDateFormatString(String instantString) {
if (instantString == null)
return null;
String time = instantString;
DateTimeFormatter formatter = null; DateTimeFormatter formatter = null;
try { try {
if (!time.endsWith(NO_TIME)) { formatter = DateTimeFormatter.ofPattern(DATE_FORMAT + " " + TIME_FORMAT);
formatter = DateTimeFormatter.ofPattern(DATE_FORMAT + " " + TIME_FORMAT);
} else {
time = time.replace(NO_TIME, "");
formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
}
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Parsing error: ", e); LOG.warn("Parsing error: ", e);
} }
try { try {
if (formatter != null) if (formatter != null) {
return dateTime.format(formatter); LocalDateTime instant = LocalDateTime.parse(time);
return formatter.format(instant);
}
} catch (Exception e) { } catch (Exception e) {
LOG.warn("Date format error: ", e); LOG.warn("Date format error: ", e);
e.printStackTrace();
} }
return dateTime.toString(); return instantString;
} }
// /** // /**
@ -917,16 +932,18 @@ public class ConvertToDataValueObjectModel {
if (keyLower.contains(possibleDate)) { if (keyLower.contains(possibleDate)) {
try { try {
LOG.debug("value " + value + " is instance of: " + value.getClass()); LOG.debug("value " + value + " is instance of: " + value.getClass());
Document documentDate = new Document((Map) value);
String jsonValue = documentDate.toJson();
LOG.debug("jsonValue " + value);
// LocalDate asDate = String asDate = instantToDateFormatString(value.toString());
// objectMapper.readerFor(LocalDate.class).readValue(jsonValue); // Document documentDate = new Document((Map) value);
// String jsonValue = documentDate.toJson();
LocalDateTime asDate = org.gcube.application.geoportal.client.utils.Serialization.read(jsonValue, // LOG.debug("jsonValue " + value);
LocalDateTime.class); //
LOG.info("Casted as date: " + asDate); // // LocalDate asDate =
// // objectMapper.readerFor(LocalDate.class).readValue(jsonValue);
//
// LocalDateTime asDate = org.gcube.application.geoportal.client.utils.Serialization.read(jsonValue,
// LocalDateTime.class);
// LOG.info("Casted as date: " + asDate);
value = asDate; value = asDate;
break; break;
} catch (Exception e) { } catch (Exception e) {