Total Pageviews

2018/03/06

[commons-validator] Date-related utilies

Scenario
In real world, we may need some common functions to fulfill our requirement, such as:
1. check date string has expected format
2. convert date string to java.util.Date, and vice versa.
3. validate start and date time 


We can use commons-validator to implement this function easily.


Remember to add dependency in your pom.xml
1
2
3
4
5
 <dependency>
  <groupId>commons-validator</groupId>
  <artifactId>commons-validator</artifactId>
  <version>1.6</version>
 </dependency>

Sample code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package albert.practice.validation;

import java.util.Date;

import org.apache.commons.validator.routines.DateValidator;

public class DateUtils {

    /**
     * 檢查 date string 格式是否符合 yyyy/MM/dd 格式.
     * 
     * @param dateString
     */
     public static void validateDate(String dateString) {
         if (!DateValidator.getInstance().isValid(dateString, "yyyy/MM/dd")) {
             throw new IllegalArgumentException("Valid date format is yyyy/MM/dd");
         }
     }

    /**
     * 檢查 date string 格式是否符合 yyyy/MM/dd 格式, 若符合則回傳 java.util.Date
     * 
     * @param dateString
     *            ex. 2017/9/14
     * @return java.util.Date
     */
     public static Date convertToDate(String dateString) {
         validateDate(dateString);
         return DateValidator.getInstance().validate(dateString, "yyyy/MM/dd");
     }

    /**
     * 將 Date 轉成指定格式 (ex. yyyy/MM/dd)的 date string.
     * 
     * @param date
     *            java.util.Date
     * @param pattern
     *            ex. yyyy/MM/dd
     * @return date String
     */
     public static String convertToString(Date date, String pattern) {
         return DateValidator.getInstance().format(date, pattern);
     }

    /**
     * 時間起迄檢查.
     * 
     * @param startDate
     *            開始日期
     * @param endDate
     *            結束日期
     */
     public static void compareDays(Date startDate, Date endDate) {
         int compare = DateValidator.getInstance().compareDates(startDate, endDate, null);
         if (compare > 0) {
             throw new IllegalArgumentException("結束時間不可早於開始時間");
         }
     }
}


Test class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package albert.practice.validation;

import static org.junit.Assert.assertEquals;

import java.util.Date;

import org.junit.Before;
import org.junit.Test;

public class DateUtilsTest {

    private String validDate = "";
    private String invalidDate = "";
    private String startDate = "";
    private String endDate = "";
    private String invalidEndDate = "";

    @Before
    public void setup() {
        validDate = "2017/09/30";
        invalidDate = "2017/09/31";

        startDate = "2017/9/14";
        endDate = "2017/9/15";
        invalidEndDate = "2017/9/11";
    }

    @Test(expected = IllegalArgumentException.class)
    public void testInvalidDate() {
        DateUtils.validateDate(invalidDate);
    }

    @Test
    public void testValidDate() {
        DateUtils.validateDate(validDate);
    }

    @Test
    public void testDateConversion() {
        Date date = DateUtils.convertToDate(validDate);
        String dateString = DateUtils.convertToString(date, "yyyy/MM/dd");
        assertEquals("2017/09/30", dateString);
    }

    @Test
    public void testValidStartAndEndDate() {
        DateUtils.compareDays(DateUtils.convertToDate(startDate), DateUtils.convertToDate(endDate));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testInvalidStartAndEndDate() {
        DateUtils.compareDays(DateUtils.convertToDate(startDate), DateUtils.convertToDate(invalidEndDate));
    }

}



No comments: