关闭

在JAVA中如何检查字符串日期的合法性

170人阅读 评论(0) 收藏 举报

在JAVA中如何检查字符串日期的合法性

文/陈刚 from www.chengang.com.cn 2005-11-04 


  在SWT中还没有日期控件,所以我的日期是用文本框手工输入的,这时就需要验证输入的日期是否合法。在之前我是复制很久以前写的一段代码,它的解决方法是分解日期字串,然后检查,这很笨且不严格。正确的方案应该是利用DateFormat类来检查,如下所示:
 
public final class StringUtil {
    private static Calendar calendar = Calendar.getInstance();
    private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static DateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static {
        dateFormat.setLenient(false);//这个的功能是不把1996-13-3 转换为1997-1-3
        dateTimeFormat.setLenient(false);
    }
 
    public static Date parseStringToDate(String dateStr) {
        if (dateStr == null || dateStr.trim().equals(""))
            return null;
        dateStr = dateStr.trim(); 
        Date date = null;
        try {
            if (dateStr.length() <= 10) {
                date = dateFormat.parse(dateStr);
            } else {
                date = dateTimeFormat.parse(dateStr);
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("你输入的日期不合法,请重新输入");
        }
        return date;
    }
}

 
这个方法的Test Case也一并给出如下:
 
public class TestStringUtil extends TestCase {
    public void testParseStringToDate() {
        {
            Calendar c = Calendar.getInstance();
            c.clear();
            c.set(2000, 1, 3, 0, 0, 0);
            Date d = StringUtil.parseStringToDate("2000-02-03");
            assertEquals(c.getTime().getTime(), d.getTime());
        }
        {
            Calendar c = Calendar.getInstance();
            c.clear();
            c.set(2000, 1, 3, 11, 22, 33);
            Date d = StringUtil.parseStringToDate("2000-02-03 11:22:33");
            assertEquals(c.getTime().getTime(), d.getTime());
        }
        {
            Calendar c = Calendar.getInstance();
            c.clear();
            c.set(2000, 1, 3, 0, 0, 0);
            Date d = StringUtil.parseStringToDate("2000-02-3");
            assertEquals(c.getTime().getTime(), d.getTime());
        }
        {
            Calendar c = Calendar.getInstance();
            c.clear();
            c.set(2000, 1, 3, 1, 2, 3);
            Date d = StringUtil.parseStringToDate("2000-02-03 1:02:03");
            assertEquals(c.getTime().getTime(), d.getTime());
        }
        {
            assertNull(StringUtil.parseStringToDate(""));
            assertNull(StringUtil.parseStringToDate("  "));
            assertNull(StringUtil.parseStringToDate(null));
        }
    }
    public void testParseStringToDateException() {
        testParseStringToDateException("2000");
        testParseStringToDateException("2000-13-03");
        testParseStringToDateException("2000-02-03 51:02:03");
        testParseStringToDateException("2000-02-03 01:f2:03");
        testParseStringToDateException("2000-02-03 01:02");
        testParseStringToDateException("2000-02-03 01:02:222");
    }
    private void testParseStringToDateException(String dateStr) {
        try {
            StringUtil.parseStringToDate(dateStr);
            fail("末抛出异常");
        } catch (Exception e) {
            assertTrue(true);
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17902次
    • 积分:297
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:29篇
    • 译文:0篇
    • 评论:0条