---
title: "Light-commons的ConvertUtils更强大了"
author: "jasongreen"
date: "2008-08-31T00:17:47Z"
tags: ["Google", "SVN", "thread"]
categories: ["企业架构"]
slug: "light-commons-convertutils"
draft: false
lastmod: "2008-08-31T00:17:47Z"
id: 234924
url: "https://jasongreen.iteye.com/blog/234924"
---

直接看浓缩版的TestCase吧，[完整版TestCase点这里](http://code.google.com/p/light-commons/source/browse/trunk/lightcommons/src/test/java/org/lightcommons/util/ConvertUtilsTest.java)


```java

CommonsGlobal.setLocale(Locale.CHINA);
Date date = ConvertUtils.dateOf("2008-02-29");
date = ConvertUtils.dateOf("2008-02-29 12:30:55");
date = ConvertUtils.dateOf("15:20:33");
assertEquals("2008-02-29", ConvertUtils.toString(date));

CommonsGlobal.setLocale(Locale.ENGLISH);
date = ConvertUtils.dateOf("02/29/2008");
date = ConvertUtils.dateOf("02/29/2008 12:30:55");
date = ConvertUtils.dateOf("15:20:33");
assertEquals("02/29/2008", ConvertUtils.toString(date));

assertEquals(Locale.CHINESE, ConvertUtils.localeOf("zh"));
assertEquals(Locale.CHINA, ConvertUtils.localeOf("zh_CN"));
assertEquals(Locale.ENGLISH, ConvertUtils.localeOf("en"));
assertEquals(Locale.US, ConvertUtils.localeOf("en_US"));

assertTrue(ConvertUtils.booleanOf("True"));
assertTrue(ConvertUtils.booleanOf("YES"));
assertTrue(ConvertUtils.booleanOf("y"));
assertTrue(ConvertUtils.booleanOf("on"));
assertFalse(ConvertUtils.booleanOf("FALSE"));
assertFalse(ConvertUtils.booleanOf("No"));
assertFalse(ConvertUtils.booleanOf("n"));
assertFalse(ConvertUtils.booleanOf("off"));
assertNull(ConvertUtils.booleanOf("Null"));
assertNull(ConvertUtils.booleanOf("Nil"));
assertNull(ConvertUtils.booleanOf("N/A"));

```


CommonsGlobal.setLocal() 只在当前线程有效，不存在thread-unsafe问题。
日期类型，各个地区都有自己的习惯，所以ConvertUtils现在以Locale作为日期转换的要素。开发人员也可以为不同地区的用户提供Date类型的本地化显示、录入方式。

未提供每日构建版下载，有兴趣的朋友可以：

> svn checkout http://light-commons.googlecode.com/svn/trunk/lightcommons lightcommons
> cd lightcommons
> mvn package 或 mvn install