From 3db12af23af8a237a1119e5adbe95d38d5fac0d1 Mon Sep 17 00:00:00 2001 From: ashitsalesforce Date: Tue, 13 Feb 2024 22:06:29 -0800 Subject: [PATCH] code cleanup of DateTimeConverter - remove wrapper constructor - remove wrapper methods --- .../dao/database/DatabaseContext.java | 2 +- .../dataloader/dyna/DateOnlyConverter.java | 10 ++-- .../dataloader/dyna/DateTimeConverter.java | 48 ++++--------------- .../dataloader/dyna/DateConverterTest.java | 10 ++-- 4 files changed, 19 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/salesforce/dataloader/dao/database/DatabaseContext.java b/src/main/java/com/salesforce/dataloader/dao/database/DatabaseContext.java index 0fbfb135..8ecdb96f 100644 --- a/src/main/java/com/salesforce/dataloader/dao/database/DatabaseContext.java +++ b/src/main/java/com/salesforce/dataloader/dao/database/DatabaseContext.java @@ -289,7 +289,7 @@ private long getTimeInMillis(TimeZone tz, Object paramValue) { return ((Date)paramValue).getTime(); } else if(paramValue instanceof String) { - Calendar cal = (Calendar)new DateTimeConverter(tz).convert(java.util.Calendar.class, paramValue); + Calendar cal = (Calendar)new DateTimeConverter(tz, false).convert(java.util.Calendar.class, paramValue); return cal.getTimeInMillis(); } else { throw new IllegalArgumentException(Messages.getFormattedString("DatabaseDAO.errorParamMappingType", paramValue.getClass().getName())); diff --git a/src/main/java/com/salesforce/dataloader/dyna/DateOnlyConverter.java b/src/main/java/com/salesforce/dataloader/dyna/DateOnlyConverter.java index 67909a80..ad86affe 100644 --- a/src/main/java/com/salesforce/dataloader/dyna/DateOnlyConverter.java +++ b/src/main/java/com/salesforce/dataloader/dyna/DateOnlyConverter.java @@ -34,19 +34,15 @@ public class DateOnlyConverter extends DateTimeConverter implements Converter { - public DateOnlyConverter(TimeZone tz) { - super(tz); - } - public DateOnlyConverter(TimeZone tz, boolean useEuroDateFormat) { super(tz, useEuroDateFormat); } - Calendar getCalendar(TimeZone tz) { + protected Calendar getCalendar(TimeZone tz) { return DateOnlyCalendar.getInstance(tz); } - Calendar getNAValueCalendar() { + protected Calendar getNAValueCalendar() { return NADateOnlyCalendarValue.getInstance(); } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/main/java/com/salesforce/dataloader/dyna/DateTimeConverter.java b/src/main/java/com/salesforce/dataloader/dyna/DateTimeConverter.java index 391ab297..961edd9b 100644 --- a/src/main/java/com/salesforce/dataloader/dyna/DateTimeConverter.java +++ b/src/main/java/com/salesforce/dataloader/dyna/DateTimeConverter.java @@ -49,22 +49,11 @@ public class DateTimeConverter implements Converter { final boolean useEuroDates; final TimeZone timeZone; - public DateTimeConverter(TimeZone tz) { - this(tz, false); - - } - public DateTimeConverter(TimeZone tz, boolean useEuroDateFormat) { this.timeZone = tz; this.useEuroDates = useEuroDateFormat; } - Calendar parseDate(TimeZone tz, String dateString, String pattern) { - final DateFormat df = new SimpleDateFormat(pattern); - df.setTimeZone(tz); - return parseDate(dateString, df); - } - private Calendar parseDate(String dateString, DateFormat fmt) { final ParsePosition pos = new ParsePosition(0); fmt.setLenient(false); @@ -78,25 +67,6 @@ private Calendar parseDate(String dateString, DateFormat fmt) { return null; } - /** - * Attempts to parse a date string using the given formatting patterns - * - * @param dateString - * The date string to parse - * @param patterns - * Patterns to try. These will be used in the constructor for SimpleDateFormat - * @return A Calendar object representing the given date string - */ - private Calendar tryParse(TimeZone tz, String dateString, String... patterns) { - if (patterns == null) return null; - for (String pattern : patterns) { - Calendar cal = parseDate(tz, dateString, pattern); - if (cal != null) return cal; - } - return null; - } - - @Override @SuppressWarnings({ "rawtypes", "unchecked" }) public Object convert(Class type, Object value) { @@ -107,16 +77,15 @@ public Object convert(Class type, Object value) { if(value instanceof NATextValue) { return getNAValueCalendar(); } + + if (value instanceof Calendar) { return value; } Calendar cal = getCalendar(this.timeZone); - if (value instanceof Date) { cal.setTimeInMillis(((Date)value).getTime()); return cal; } - if (value instanceof Calendar) { return value; } - String dateString = value.toString().trim(); int len = dateString.length(); @@ -128,8 +97,10 @@ public Object convert(Class type, Object value) { timeZoneForValue = GMT_TZ; } - for (String basePattern : useEuroDates ? supportedEuropeanPatterns : supportedRegularPatterns) { - cal = tryParse(timeZoneForValue, dateString, basePattern); + for (String pattern : useEuroDates ? supportedEuropeanPatterns : supportedRegularPatterns) { + final DateFormat df = new SimpleDateFormat(pattern); + df.setTimeZone(timeZoneForValue); + cal = parseDate(dateString, df); if (cal != null) return cal; } @@ -146,11 +117,12 @@ public Object convert(Class type, Object value) { throw new ConversionException("Failed to parse date: " + value); } - Calendar getCalendar(TimeZone timezone) { + // NOTE: Always use this method to get Calendar instance + protected Calendar getCalendar(TimeZone timezone) { return Calendar.getInstance(timezone); } - Calendar getNAValueCalendar() { + protected Calendar getNAValueCalendar() { return NACalendarValue.getInstance(); } @@ -159,7 +131,7 @@ Calendar getNAValueCalendar() { * These patterns are a subset of patterns supported by Java text.SimpleDateFormat * https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html */ - static List getSupportedPatterns(boolean europeanDates) { + private static List getSupportedPatterns(boolean europeanDates) { List basePatterns = new ArrayList(); diff --git a/src/test/java/com/salesforce/dataloader/dyna/DateConverterTest.java b/src/test/java/com/salesforce/dataloader/dyna/DateConverterTest.java index 1e184d9d..2e6dd9d6 100644 --- a/src/test/java/com/salesforce/dataloader/dyna/DateConverterTest.java +++ b/src/test/java/com/salesforce/dataloader/dyna/DateConverterTest.java @@ -193,7 +193,7 @@ public void testFullBasicFormat() { public void testDateConverterWithNull() { Calendar calDate; - DateTimeConverter converter = new DateTimeConverter(TZ); + DateTimeConverter converter = new DateTimeConverter(TZ, false); // test null and empty string calDate = (Calendar) converter.convert(null, null); @@ -213,7 +213,7 @@ public void testDateConverterWithNull() { public void testDateClosureUnderCalendarConversion() { Calendar calDate; - DateTimeConverter converter = new DateTimeConverter(TZ); + DateTimeConverter converter = new DateTimeConverter(TZ, false); // if we pass in a calendar, should get the same Calendar back Calendar testCalDate = Calendar.getInstance(); @@ -650,9 +650,9 @@ public void testDateConverterNegative() { // https://developer.salesforce.com/docs/atlas.en-us.dataLoader.meta/dataLoader/supported_data_types.htm @Test public void testUserSpecifiedTimeZoneIsUsed() throws Exception { - DateTimeConverter AsianTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("Asia/Tokyo")); - DateTimeConverter USTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("America/Los_Angeles")); - DateTimeConverter GMTTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("GMT")); + DateTimeConverter AsianTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("Asia/Tokyo"), false); + DateTimeConverter USTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("America/Los_Angeles"), false); + DateTimeConverter GMTTZDateConverter = new DateTimeConverter(TimeZone.getTimeZone("GMT"), false); DateOnlyConverter AsianTZDateOnlyConverter = new DateOnlyConverter(TimeZone.getTimeZone("Asia/Tokyo"), false); DateOnlyConverter USTZDateOnlyConverter = new DateOnlyConverter(TimeZone.getTimeZone("America/Los_Angeles"), false); DateOnlyConverter GMTTZDateOnlyConverter = new DateOnlyConverter(TimeZone.getTimeZone("GMT"), false);