|
| 1 | +package org.commcare.util; |
| 2 | + |
| 3 | +import org.commcare.modern.util.Pair; |
| 4 | + |
| 5 | +import java.text.ParseException; |
| 6 | +import java.text.SimpleDateFormat; |
| 7 | +import java.util.Date; |
| 8 | +import java.util.Locale; |
| 9 | + |
| 10 | +import javax.annotation.Nullable; |
| 11 | + |
| 12 | +public class DateRangeUtils { |
| 13 | + |
| 14 | + // Changing this will require changing this format on ES end as well |
| 15 | + public static final String DATE_RANGE_ANSWER_PREFIX = "__range__"; |
| 16 | + public static final String DATE_RANGE_ANSWER_DELIMITER = "__"; |
| 17 | + public static final String DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER = " to "; |
| 18 | + private static final String DATE_FORMAT = "yyyy-MM-dd"; |
| 19 | + |
| 20 | + /** |
| 21 | + * @param humanReadableDateRange human readable fomat for date range as 'startDate to endDate' |
| 22 | + * @return a Pair of start time and end time that can be supplied to MaterialDatePicker to set a date range, |
| 23 | + * @throws ParseException if the given humanReadableDateRange is not in 'yyyy-mm-dd to yyyy-mm-dd' format |
| 24 | + */ |
| 25 | + @Nullable |
| 26 | + public static Pair<Long, Long> parseHumanReadableDate(String humanReadableDateRange) throws ParseException { |
| 27 | + if (humanReadableDateRange.contains(DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER)) { |
| 28 | + String[] humanReadableDateRangeSplit = humanReadableDateRange.split(DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER); |
| 29 | + if (humanReadableDateRangeSplit.length == 2) { |
| 30 | + SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US); |
| 31 | + Date startDate = sdf.parse(humanReadableDateRangeSplit[0]); |
| 32 | + Date endDate = sdf.parse(humanReadableDateRangeSplit[1]); |
| 33 | + return new Pair<>(getTimeFromDateOffsettingTz(startDate), getTimeFromDateOffsettingTz(endDate)); |
| 34 | + } |
| 35 | + } |
| 36 | + throw new ParseException("Argument " + humanReadableDateRange + " should be formatted as 'yyyy-mm-dd to yyyy-mm-dd'", 0); |
| 37 | + } |
| 38 | + |
| 39 | + private static Long getTimeFromDateOffsettingTz(Date date) { |
| 40 | + return date.getTime() - date.getTimezoneOffset() * 60 * 1000; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Formats __range__startDate__endDate as 'startDate to EndDate' |
| 45 | + * |
| 46 | + * @param dateRangeAnswer A date range value in form of '__range__startDate__endDate' |
| 47 | + * @return human readable format 'startDate to EndDate' for given dateRangeAnswer |
| 48 | + */ |
| 49 | + public static String getHumanReadableDateRange(String dateRangeAnswer) { |
| 50 | + if (dateRangeAnswer != null && dateRangeAnswer.startsWith(DATE_RANGE_ANSWER_PREFIX)) { |
| 51 | + String[] dateRangeSplit = dateRangeAnswer.split(DATE_RANGE_ANSWER_DELIMITER); |
| 52 | + if (dateRangeSplit.length == 4) { |
| 53 | + return getHumanReadableDateRange(dateRangeSplit[2], dateRangeSplit[3]); |
| 54 | + } |
| 55 | + } |
| 56 | + return dateRangeAnswer; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + // Formats as 'startDate to endDate' |
| 61 | + public static String getHumanReadableDateRange(String startDate, String endDate) { |
| 62 | + return startDate + DATE_RANGE_ANSWER_HUMAN_READABLE_DELIMITER + endDate; |
| 63 | + } |
| 64 | + |
| 65 | + // Formats as '__range__startDate__endDate' |
| 66 | + public static String formatDateRangeAnswer(String startDate, String endDate) { |
| 67 | + return DATE_RANGE_ANSWER_PREFIX + startDate + DATE_RANGE_ANSWER_DELIMITER + endDate; |
| 68 | + } |
| 69 | + |
| 70 | + public static String formatDateRangeAnswer(String humanReadableDateRange) throws ParseException { |
| 71 | + Pair<Long, Long> selection = DateRangeUtils.parseHumanReadableDate(humanReadableDateRange); |
| 72 | + String startDate = DateRangeUtils.getDateFromTime(selection.first); |
| 73 | + String endDate = DateRangeUtils.getDateFromTime(selection.second); |
| 74 | + return DATE_RANGE_ANSWER_PREFIX + startDate + DATE_RANGE_ANSWER_DELIMITER + endDate; |
| 75 | + } |
| 76 | + |
| 77 | + // Convers given time as yyyy-mm-dd |
| 78 | + public static String getDateFromTime(Long time) { |
| 79 | + return new SimpleDateFormat(DATE_FORMAT, Locale.US).format(new Date(time)); |
| 80 | + } |
| 81 | +} |
0 commit comments