子在川上曰

  逝者如斯夫不舍昼夜
随笔 - 71, 文章 - 0, 评论 - 915, 引用 - 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);
        }
    }
}

posted on 2006-03-07 14:39 陈刚 阅读(3517) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航: