Skip to content

Commit

Permalink
✅ Pass dateFormat in get date for calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
allan-on committed Sep 15, 2020
1 parent 0344d71 commit 329d3c5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 329d3c5

Please sign in to comment.