From 329d3c59cc5b5adbd0bce1b6445f64c032eac5c4 Mon Sep 17 00:00:00 2001 From: Allan O Date: Tue, 15 Sep 2020 18:20:18 +0300 Subject: [PATCH] :white_check_mark: Pass dateFormat in get date for calculation --- .../vijay/jsonwizard/rules/RulesEngineDateUtil.java | 7 ++++--- .../main/java/com/vijay/jsonwizard/utils/Utils.java | 3 +-- .../java/com/vijay/jsonwizard/utils/UtilsTest.java | 11 ++++++++--- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java index 858c0d806..18530581b 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/rules/RulesEngineDateUtil.java @@ -3,6 +3,7 @@ import android.support.annotation.NonNull; import android.text.TextUtils; +import com.vijay.jsonwizard.domain.Form; import com.vijay.jsonwizard.utils.FormUtils; import com.vijay.jsonwizard.utils.Utils; @@ -78,7 +79,7 @@ public String getWeeksAndDaysFromDays(Integer days) { } public String formatDate(String dateString, String duration) { - LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); int result = 0; String cleanDuration = duration.trim().toLowerCase(); @@ -128,7 +129,7 @@ public String addDuration(String durationString) { * @return String with date */ public String addDuration(String dateString, String durationString) { - LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); String[] durationArr = getDurationArray(durationString); for (String duration : durationArr) { @@ -181,7 +182,7 @@ public String subtractDuration(String durationString) { */ public String subtractDuration(String dateString, String durationString) { - LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString), "-")); + LocalDate date = new LocalDate(Utils.reverseDateString(Utils.getDateFormattedForCalculation(dateString, Form.getDatePickerDisplayFormat()), "-")); String[] durationArr = getDurationArray(durationString); diff --git a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java index 720205622..9ddae8dd1 100644 --- a/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java +++ b/android-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/Utils.java @@ -107,8 +107,7 @@ public static String reverseDateString(String str, String delimiter) { return strr[2] + "-" + strr[1] + "-" + strr[0]; } - public static String getDateFormattedForCalculation(String date) { - String datePickerDisplayFormat = Form.getDatePickerDisplayFormat(); + public static String getDateFormattedForCalculation(String date, String datePickerDisplayFormat) { if (StringUtils.isNotBlank(datePickerDisplayFormat)) { return formatDateToPattern(date, datePickerDisplayFormat, DatePickerFactory.DATE_FORMAT.toPattern()); } else diff --git a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java index f81af97d4..6a7ac37e7 100644 --- a/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java +++ b/android-json-form-wizard/src/test/java/com/vijay/jsonwizard/utils/UtilsTest.java @@ -334,11 +334,16 @@ public void testReverseDateStringShouldReturnReverseDateString() { @Test public void getDateForCalculationShouldReturnCorrectDateString() { String date = "20-12-2021"; - Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date)); + Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date, null)); + } + + @Test + public void getDateForCalculationReturnsDateStringWhenDisplayFormatIsSet() { Form form = new Form(); form.setDatePickerDisplayFormat("dd MMM yyyy"); - date = "20 DEC 2021"; - Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date)); + String date = "20 DEC 2021"; + Assert.assertEquals("20-12-2021", Utils.getDateFormattedForCalculation(date, Form.getDatePickerDisplayFormat())); + form.setDatePickerDisplayFormat(null); // To not pollute other tests } @Test