Skip to content

Commit

Permalink
Merge pull request #6274 from effective-webwork/default-date-format
Browse files Browse the repository at this point in the history
Move default date format to constant
  • Loading branch information
solth authored Oct 28, 2024
2 parents 2b12938 + 137a9d1 commit b68c5d5
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public class StringConstants {
public static final String SEMICOLON_DELIMITER = "; ";
public static final String EDIT_FORM_SAVE = "editForm:save";

// patterns
public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.kitodo.data.elasticsearch.index.type;

import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -138,7 +140,7 @@ private void getAdditionalProperties(Map<String, Object> jsonObject, BaseBean pr
*/
String getFormattedDate(Date date) {
if (Objects.nonNull(date)) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
return dateFormat.format(date);
}
return "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -260,7 +261,7 @@ public void shouldCreateDocuments() {
}

private String formatDate(Date date) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
return dateFormat.format(date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -338,7 +339,7 @@ public void shouldCreateSecondDocument() throws Exception {
@Test
public void shouldCreateThirdDocument() throws Exception {
ProjectType processType = new ProjectType();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);

Project project = prepareData().get(2);
Map<String, Object> actual = processType.createDocument(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -195,7 +196,7 @@ public void shouldCreateDocuments() {
}

private String formatDate(Date date) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
return dateFormat.format(date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.kitodo.production.forms;

import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.MessageFormat;
Expand Down Expand Up @@ -364,7 +366,7 @@ public void saveSelectedColumns() {
*/
public String getFormattedDate(Date date) {
if (Objects.nonNull(date)) {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
return dateFormat.format(date);
}
return "";
Expand Down
8 changes: 5 additions & 3 deletions Kitodo/src/main/java/org/kitodo/production/helper/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.kitodo.production.helper;

import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.SecureRandom;
Expand Down Expand Up @@ -398,7 +400,7 @@ public static String getDateAsFormattedString(Date date) {
if (Objects.isNull(date)) {
return "-";
} else {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
return dateFormat.format(date);
}
}
Expand All @@ -416,12 +418,12 @@ public static Date parseDateFromFormattedString(String date) {
return null;
}
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT);
LocalDateTime localDate = LocalDateTime.parse(date, formatter);
Instant instant = localDate.toInstant(ZoneOffset.UTC);
return Date.from(instant);
} catch (DateTimeParseException e) {
logger.info("invalid date format (yyyy-MM-dd HH:mm:ss) for date string: '" + date + "'");
logger.info("invalid date format '{}' for date string: '{}'", DEFAULT_DATE_FORMAT, date);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.kitodo.production.helper;

import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -131,7 +133,7 @@ private static Task getCurrentTask(Property property) throws DAOException {
}

private static Date getCreationDate(Property property) throws ParseException {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(property.getValue().substring(1, 20));
return new SimpleDateFormat(DEFAULT_DATE_FORMAT).parse(property.getValue().substring(1, 20));
}

private static String getWikiFieldMessage(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

package org.kitodo.production.security;

import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

Expand Down Expand Up @@ -73,7 +75,7 @@ public LocalDateTime getLastRequest() {
* @return The lastRequest.
*/
public String getLastRequestAsString() {
return lastRequest.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
return lastRequest.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
import static org.elasticsearch.index.query.QueryBuilders.nestedQuery;
import static org.kitodo.constants.StringConstants.DEFAULT_DATE_FORMAT;
import static org.kitodo.data.database.enums.CorrectionComments.NO_CORRECTION_COMMENTS;
import static org.kitodo.data.database.enums.CorrectionComments.NO_OPEN_CORRECTION_COMMENTS;
import static org.kitodo.data.database.enums.CorrectionComments.OPEN_CORRECTION_COMMENTS;
Expand Down Expand Up @@ -2223,7 +2224,7 @@ public static String getPropertyValue(ProcessDTO process, String propertyName) {
*/
public static String getProcessDuration(ProcessDTO process) {
String creationDateTimeString = process.getCreationDate();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT);
LocalDateTime createLocalDate = LocalDateTime.parse(creationDateTimeString, formatter);
Duration duration = Duration.between(createLocalDate, LocalDateTime.now());
return String.format("%sd; %sh", duration.toDays(),
Expand Down

0 comments on commit b68c5d5

Please sign in to comment.