I have a couple of general purpose LocalDateTime parser methods I wrote to handle Dates flowing between a number of legacy XML based processing pipelines over to a modern Spring Boot, Mongo applications. Because both the legacy and modern applications are set in stone, I'm stuck with a narrow conversion of legacy String dates to LocalDateTime. That said I wanted to present it for review and perhaps improve the code. The various legacy String Dates we have are :
// 200701 - 6 yyyyMM
// 032007 - 6 MMyyyy
// 2007-01 - 7 yyyy-MM
// 03-2007 - 7 MM-yyyy
// 03012007 - 8 MMddyyyy
// 20070301 - 8 yyyyMMdd
// 03-01-2007 - 10 MM-dd-yyyy
// 2007-03-01 - 10 MM-dd-yyyy
And the methods I came up with are : The primary method is parseDate(), I just pass in the legacy String Date and basically have it figure out the length and then try and convert it to LocalDateTime. It works and all the JUnit tests pass.
public LocalDateTime parseDate(String dateString) {
checkArgument(!Strings.isNullOrEmpty(dateString), "Date [" + dateString + "] is empty of NULL!");
LOG.debug("parseDate.................................Date [{}]", dateString);
LocalDateTime ldateResults = null;
dateString = dateString.replace("/","-");
switch (dateString.length())
{
case 6:
try { ldateResults = getLocalDateTimeFromString ( dateString, "MMyyyy" ); }
catch ( Exception ltwoXcp ) {
LOG.warn("Date [{}] failed with format [MMyyyy], trying another", dateString);
try { ldateResults = getLocalDateTimeFromString ( dateString, "yyyyMM" ); }
catch ( Exception loneXcp ) {
LOG.warn("Date [{}] failed with format [yyyyMM], trying another", dateString);
}
}
break;
case 7:
if ( dateString.indexOf("-") > 3 ) {
try { ldateResults = getLocalDateTimeFromString ( dateString, "yyyy-MM" ); }
catch ( Exception loneXcp ) {
LOG.warn("Date [{}] failed with format [yyyy-MM], trying another", dateString);
}
try { ldateResults = getLocalDateTimeFromString ( dateString, "MM-yyyy" ); }
catch ( Exception ltwoXcp ) {
LOG.warn("Date [{}] failed with format [MM-yyyy], trying another", dateString);
}
}
break;
case 8:
try { ldateResults = getLocalDateTimeFromString ( dateString, "MMddyyyy" ); }
catch ( Exception loneXcp ) {
LOG.warn("Date [{}] failed with format [MMddyyyy], trying another", dateString);
try { ldateResults = getLocalDateTimeFromString ( dateString, "yyyyMMdd" ); }
catch ( Exception ltwoXcp ) {
LOG.warn("Date [{}] failed with format [yyyyMMdd], trying another", dateString);
}
}
break;
case 10:
try { ldateResults = getLocalDateTimeFromString ( dateString, "MM-dd-yyyy" ); }
catch ( Exception loneXcp ) {
LOG.warn("Date [{}] failed with format [MM-dd-yyyy], trying another", dateString);
try { ldateResults = getLocalDateTimeFromString ( dateString, "yyyy-MM-dd" ); }
catch ( Exception ltwoXcp ) {
LOG.warn("Date [{}] failed with format [yyyy-MM-dd], trying another", dateString);
}
}
break;
}
return ldateResults;
}
public LocalDateTime getLocalDateTimeFromString(String dateString, String dateFormat) {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(dateFormat);
if ( dateFormat.length() == 6 ) {
LocalDate localDate = YearMonth.parse(dateString, dateFormatter).atDay(1);
return localDate.atStartOfDay();
}
return LocalDate.parse(dateString, dateFormatter).atStartOfDay();
}
public String getStringFromLocalDateTime(LocalDateTime date, String dateFormat) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
return formatter.format(date);
}
Here are some JUnit tests that run :
@Test
public void parseDateReturnsCorrectyyyyMM() {
String dateString="200703";
LocalDateTime ldtValue = null;
try {
ldtValue = dateUtils.parseDate(dateString);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
if ( ldtValue != null ) {
dateString = dateUtils.getStringFromLocalDateTime(ldtValue,"MM/dd/yyyy");
}
assertEquals("03/01/2007", dateString);
}
@Test
public void parseDateReturnsCorrectMMyyyy() {
String dateString="032007";
LocalDateTime ldtValue = null;
try {
ldtValue = dateUtils.parseDate(dateString);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
if ( ldtValue != null ) {
dateString = dateUtils.getStringFromLocalDateTime(ldtValue,"MM/dd/yyyy");
}
assertEquals("03/01/2007", dateString);
}
@Test
public void parseDateReturnsCorrectyyyyMMdd() {
String dateString="20070301";
LocalDateTime ldtValue = null;
try {
ldtValue = dateUtils.parseDate(dateString);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
if ( ldtValue != null ) {
dateString = dateUtils.getStringFromLocalDateTime(ldtValue,"MM/dd/yyyy");
}
assertEquals("03/01/2007", dateString);
}
@Test
public void parseDateReturnsCorrectMMddyyyy() {
String dateString="03012007";
LocalDateTime ldtValue = null;
try {
ldtValue = dateUtils.parseDate(dateString);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
if ( ldtValue != null ) {
dateString = dateUtils.getStringFromLocalDateTime(ldtValue,"MM/dd/yyyy");
}
assertEquals("03/01/2007", dateString);
}
@Test
public void parseDateReturnsCorrectMM_dd_yyyy() {
String dateString="03-01-2007";
LocalDateTime ldtValue = null;
try {
ldtValue = dateUtils.parseDate(dateString);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
if ( ldtValue != null ) {
dateString = dateUtils.getStringFromLocalDateTime(ldtValue,"MM/dd/yyyy");
}
assertEquals("03/01/2007", dateString);
}
@Test
public void parseDateReturnsCorrectyyyy_MM_dd() {
String dateString="2007-03-01";
LocalDateTime ldtValue = null;
try {
ldtValue = dateUtils.parseDate(dateString);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
if ( ldtValue != null ) {
dateString = dateUtils.getStringFromLocalDateTime(ldtValue,"MM/dd/yyyy");
}
assertEquals("03/01/2007", dateString);
}
Tia adym
case 7
theif
should probably only surround the firsttry
block and ingetLocalDateTimeFromString
theif
should probably cover the lengths6
and7
. \$\endgroup\$