当前位置: 代码迷 >> J2SE >> java识别1900年时间的有关问题【急】
  详细解决方案

java识别1900年时间的有关问题【急】

热度:102   发布时间:2016-04-23 21:55:36.0
求救java识别1900年时间的问题【急,在线等】
本帖最后由 kerafan 于 2013-08-12 15:35:23 编辑
在网上找了一个判断给定字符串是否为合法的日期时间字符串的方法。但是这个方法对1900年的时间不适用。比如19000101080000会认为是错误的时间。但是在Oracle中,这个时间是合法的。有什么办法可以识别1900年的时间。

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UTest3 {
public static void main(String args[]) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
sdf.setLenient(false);
try {
Date date = sdf.parse("19000101080000");
System.out.println(date);
} catch (ParseException e) {
System.out.println("Error");
}
}
}

------解决方案--------------------

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
 
public class UTest3 {
    public static void main(String args[]) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        sdf.setLenient(false);
        try {
         GregorianCalendar calendar = new GregorianCalendar();
         sdf.setCalendar(calendar);
            Date date = sdf.parse("19000101080000");
            System.out.println(date.toLocaleString());
        } catch (ParseException e) {
         e.printStackTrace();
            System.out.println(e.getLocalizedMessage());
        }
    }
}

输出的时间是
1900-1-1 8:05:52
这有点让我费解
------解决方案--------------------
  相关解决方案