eclipse中的Date类型转换问题

-----------------------------------------------------------------------------------------
今天遇到的问题是在eclipse下产生的,而不是浏览器的:

jsp页面中有一个域:生产日期:<input type="text" name="productInfo.productDate" value="">
其类型对性的是ProductInfo对象的productDate属性,类型为Date,在提交时,如果该域不填,则会填充null值,不会抛出异常,但是输入日期如下时,会显示错误:
2013/11/13

因为在com.opensymphony.xwork2.conversion.impl包的DateConverter类中有定义类型转换的格式:
public Object convertValue(Map<String, Object> context, Object target, Member member, String propertyName, Object value, Class toType)


该方法中默认设定为:Date result = null;
该方法中用到了一个变量:Locale locale = getLocale(context);
该变量应该是设定当地的地区的吧,没怎么详细研究,继续跟踪该方法,又有下面一段代码:

Locale locale = (Locale) context.get(ActionContext.LOCALE);
if (locale == null) {
    locale = Locale.getDefault();
}



不知道为什么我输入我系统的地区日期时显示的错误是Invalid field value for field "productInfo.productDate".
于是我猜测上面的local为null,因而调用了Locale.getDefault();
继续跟踪:

public static Locale getDefault() {
// do not synchronize this method - see 4071298
// it's OK if more than one default locale happens to be created
if (defaultLocale == null) {
    String language, region, country, variant;
    language = (String) AccessController.doPrivileged(
            new GetPropertyAction("user.language", "en"));
    // for compatibility, check for old user.region property
    region = (String) AccessController.doPrivileged(
            new GetPropertyAction("user.region"));
    if (region != null) {
    // region can be of form country, country_variant, or _variant
    int i = region.indexOf('_');
    if (i >= 0) {
        country = region.substring(0, i);
        variant = region.substring(i + 1);
    } else {
        country = region;
        variant = "";
    }
    } else {
    country = (String) AccessController.doPrivileged(
            new GetPropertyAction("user.country", ""));
    variant = (String) AccessController.doPrivileged(
            new GetPropertyAction("user.variant", ""));
    }
    defaultLocale = getInstance(language, country, variant);
}
return defaultLocale;
}



发现defaultLocale初始值就为null,最后的defaultLocale = getInstance(language, country, variant);用到的language为    language = (String) AccessController.doPrivileged(new GetPropertyAction("user.language", "en"));于是我肯定是设定了美国的时间格式,于是我输入了美国的时间格式的日期,发现没错误了

注1:我测试的是在eclipse下,得到的locale才是null,在浏览器端可以设定语言,所以浏览器的时间能否正常显示要与浏览器设定的语言(与地区)时间格式匹配就能正常显示了
注2:Date对应的只能是日期,不能是时间,输入时间一样报错
注3:我调试了一下美国的长日期发现也出错,只要把前面的星期去掉就行了,如:Wednesday, November 13, 2013把Wednesday, 去掉就可以了
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值