各位大神,我最近要比有较办公室的PDF文件和Excel文件,使用的是Java语言。要比较的项目,我已经从PDF和Excel中得到了。现在,有一个问题在于,由于两边获得的日期格式是不相同的,都是String类型的,所以,我想知道,Java中有什么API方法可以将两边的格式统一,变成一个日期?
现在,基本的格式有:
04-八月-12
11-NOV-2011
2011-03-30
28-sep-2011
2012-11-07
15-OCT-2012
11-Oct-13
10-09-2013现在,我要把里面可能的2种日期组合,进行对比,不知道,大家有没有什么好的办法?跪求各路大神帮忙啊!Java  日期

解决方案 »

  1.   

    我只对你列出的情况做了处理,有新的格式需要你自己再往里面加。    public String turn(String str) throws ParseException {
            SimpleDateFormat format = null;
            if (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{2}")) {
                format = new SimpleDateFormat("dd-MM-yy", Locale.CHINESE);
            } else if (str.matches("\\d{2}-[a-zA-Z]{3}-\\d{4}")) {
                format = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
            } else if (str.matches("\\d{4}-\\d{2}-\\d{2}")) {
                format = new SimpleDateFormat("yyyy-MM-dd");
            } else if (str.matches("\\d{2}-[a-zA-Z]{3}-\\d{2}")) {
                format = new SimpleDateFormat("dd-MMM-yy", Locale.US);
            } else if (str.matches("\\d{2}-\\d{2}-\\d{4}")) {
                format = new SimpleDateFormat("MM-dd-yyyy");
            } else {
                System.out.println("需要处理的日期格式");
                return "";
            }
            Date date = format.parse(str);
            format = new SimpleDateFormat("yyyy-MM-dd");
            return format.format(date);
        }
      

  2.   

    1楼的方式很好,正则表达式结合DateFormat处理,很方便。
      

  3.   


    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你条件里面只有
    04-八月-12
    没有
    04-八月-2013
    所以返回字符串为空,报异常。
    你添加一个条件就好了
    (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) {
                format = new SimpleDateFormat("dd-MM-yyyy", Locale.CHINESE);
            } 
      

  4.   


    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你条件里面只有
    04-八月-12
    没有
    04-八月-2013
    所以返回字符串为空,报异常。
    你添加一个条件就好了
    (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) {
                format = new SimpleDateFormat("dd-MM-yyyy", Locale.CHINESE);
            } 
    不是,我换成了那样的,也是不行啊。你看一下代码:
    String a = "04-九月-2013";
    String b = turn( a );
    System.out.println( b );
    }
    public static String turn(String str) throws ParseException 
    {         
    SimpleDateFormat format = null;         
    if (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) 
    {             
    format = new SimpleDateFormat("dd-MM-yyyy",Locale.CHINESE);    
    } 报的异常是这样的:
    Exception in thread "main" java.text.ParseException: Unparseable date: "04-九月-2013"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at Jdbc.turn(Jdbc.java:74)
    at Jdbc.main(Jdbc.java:40)
      

  5.   


    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你条件里面只有
    04-八月-12
    没有
    04-八月-2013
    所以返回字符串为空,报异常。
    你添加一个条件就好了
    (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) {
                format = new SimpleDateFormat("dd-MM-yyyy", Locale.CHINESE);
            } 
    不是,我换成了那样的,也是不行啊。你看一下代码:
    String a = "04-九月-2013";
    String b = turn( a );
    System.out.println( b );
    }
    public static String turn(String str) throws ParseException 
    {         
    SimpleDateFormat format = null;         
    if (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) 
    {             
    format = new SimpleDateFormat("dd-MM-yyyy",Locale.CHINESE);    
    } 报的异常是这样的:
    Exception in thread "main" java.text.ParseException: Unparseable date: "04-九月-2013"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at Jdbc.turn(Jdbc.java:74)
    at Jdbc.main(Jdbc.java:40)
    public static void main(String[] args) {
    String a = "04-9月-2013";
    Date b = turn(a);
    System.out.println(b);
    } public static Date turn(String str) {
    SimpleDateFormat format = null;
    // if (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) {
    format = new SimpleDateFormat("dd-MM月-yyyy", Locale.CHINESE);
    // }
    Date parse = null;
    try {
    parse = format.parse(str);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return parse;
    }
    这样就好了。。

    当然,首先注意正则是否会通过
    我的结果:
    Wed Sep 04 00:00:00 CST 2013
      

  6.   


    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你条件里面只有
    04-八月-12
    没有
    04-八月-2013
    所以返回字符串为空,报异常。
    你添加一个条件就好了
    (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) {
                format = new SimpleDateFormat("dd-MM-yyyy", Locale.CHINESE);
            } 
    不是,我换成了那样的,也是不行啊。你看一下代码:
    String a = "04-九月-2013";
    String b = turn( a );
    System.out.println( b );
    }
    public static String turn(String str) throws ParseException 
    {         
    SimpleDateFormat format = null;         
    if (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) 
    {             
    format = new SimpleDateFormat("dd-MM-yyyy",Locale.CHINESE);    
    } 报的异常是这样的:
    Exception in thread "main" java.text.ParseException: Unparseable date: "04-九月-2013"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at Jdbc.turn(Jdbc.java:74)
    at Jdbc.main(Jdbc.java:40)
    public static void main(String[] args) {
    String a = "04-9月-2013";
    Date b = turn(a);
    System.out.println(b);
    } public static Date turn(String str) {
    SimpleDateFormat format = null;
    // if (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) {
    format = new SimpleDateFormat("dd-MM月-yyyy", Locale.CHINESE);
    // }
    Date parse = null;
    try {
    parse = format.parse(str);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return parse;
    }
    这样就好了。。

    当然,首先注意正则是否会通过
    我的结果:
    Wed Sep 04 00:00:00 CST 2013
    如果测试案例是 String a = "04-九月-2013";
    还是会报异常啊!
      

  7.   


    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你好,我试了一下,我用了04-八月-2013作为测试样板,但是,这个老是抛出异常,求解救啊!
    你条件里面只有
    04-八月-12
    没有
    04-八月-2013
    所以返回字符串为空,报异常。
    你添加一个条件就好了
    (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) {
                format = new SimpleDateFormat("dd-MM-yyyy", Locale.CHINESE);
            } 
    不是,我换成了那样的,也是不行啊。你看一下代码:
    String a = "04-九月-2013";
    String b = turn( a );
    System.out.println( b );
    }
    public static String turn(String str) throws ParseException 
    {         
    SimpleDateFormat format = null;         
    if (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) 
    {             
    format = new SimpleDateFormat("dd-MM-yyyy",Locale.CHINESE);    
    } 报的异常是这样的:
    Exception in thread "main" java.text.ParseException: Unparseable date: "04-九月-2013"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at Jdbc.turn(Jdbc.java:74)
    at Jdbc.main(Jdbc.java:40)
    public static void main(String[] args) {
    String a = "04-9月-2013";
    Date b = turn(a);
    System.out.println(b);
    } public static Date turn(String str) {
    SimpleDateFormat format = null;
    // if (str.matches("\\d{2}-[\u4E00-\u9FB0]{2}-\\d{4}")) {
    format = new SimpleDateFormat("dd-MM月-yyyy", Locale.CHINESE);
    // }
    Date parse = null;
    try {
    parse = format.parse(str);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return parse;
    }
    这样就好了。。

    当然,首先注意正则是否会通过
    我的结果:
    Wed Sep 04 00:00:00 CST 2013
    如果测试案例是 String a = "04-九月-2013";
    还是会报异常啊!
    不识别中文的九的原因。。
    你用下面的方法吧。。public class Test {
    public static void main(String[] args) {
    String a = "04-九月-2013";
    a = getDateStr(a);
    Date b = turn(a);
    System.out.println(b);
    } public static Date turn(String str) {
    SimpleDateFormat format = null;
    if (str.matches("\\d{2}-\\d{1,2}[\u4E00-\u9FB0]{1}-\\d{4}")) {
    format = new SimpleDateFormat("dd-MM月-yyyy", Locale.CHINESE);
    }
    Date parse = null;
    try {
    parse = format.parse(str);
    } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return parse;
    }
    public static String getDateStr(String str){
    str=str.replaceAll("一", "1");
    str=str.replaceAll("九", "9");

    return str;
    }
    }
      

  8.   

    我写了一个 ,目前只实现了前面两种格式的转换器 , 如果要实现其他的 ,你可以自己去实现。 
    package rabitmq01.test04;import java.util.Calendar;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.Map;public class DateCompare { public static void main(String[] args) { ConverterManager cm = new ConverterManager() ;
    //注册转换器
    cm.registConverter(new Converter01()); 
    cm.registConverter(new Converter02()); 

    //测试数据
    String [] ss = {
    "04-八月-12" , 
    "11-NOV-2011" , 
    "16-十一月-13",
    "03-OCT-2031" ,
    "042-十一月-12"  , 
    "04-十九月-13"
    } ;    for (String s : ss) {

    //转换
    Date date = null;
    try {
    date = cm.parse(s);
    } catch (Exception e) {
    System.out.println("【转换错误】" + s +  "    " + e.getMessage());
    }     if(date != null)  
    System.out.printf(s + "  -->  %tF\n", date);
    else{
    System.out.println(s + "    没有找到该格式的转换器");  
    }
    }


    }

    }class ConverterManager{
    private LinkedList<Converter> converters = new LinkedList<Converter>() ; private int current = 0 ; private String str ; public Date parse(String str) throws Exception{  
    this.str = str ;
    current = -1;  
    return next() ;
    } public Date next() throws Exception{
    current++ ;
    if(current >= converters.size())
    return null ;
    return converters.get(current).transform(str , this) ;
    } public void registConverter(Converter converter){
    if(!converters.contains(converter)){
    converters.push(converter);
    }
    } } interface Converter {
    /*
     * 将源字符串转换为日期格式
     */
    Date transform(String s , ConverterManager dc ) throws Exception;}/*
     * 转换04-八月-12格式
     */
    class Converter01 implements Converter{ final String pattern = "(\\d{2})-(.{1,2})月-(\\d{2})"; 
    final Map<String , Integer> monthMap = new HashMap<String ,Integer>();
    {
    monthMap.put("一", 1);
    monthMap.put("二", 2);
    monthMap.put("三", 3);
    monthMap.put("四", 4);
    monthMap.put("五", 5);
    monthMap.put("六", 6);
    monthMap.put("七", 7);
    monthMap.put("八", 8);
    monthMap.put("九", 9);
    monthMap.put("十", 10);
    monthMap.put("十一", 11);
    monthMap.put("十二", 12);
    }
    @Override
    public Date transform(String s , ConverterManager dc ) throws Exception {
    if(s.matches(pattern)){
    String[] ss = s.replaceAll(pattern, "$1,$2,$3").split(",");
    Integer date = Integer.valueOf(ss[0]) ;
    Integer month =  monthMap.get(ss[1]) ;
    Integer year = Integer.valueOf("20" + ss[2]) ;
    if(month == null){
    throw new Exception("月份" + ss[1] + "不存在") ;
    }
    Calendar cal = Calendar.getInstance() ;
    cal.set(year, month  - 1 , date); 
    return cal.getTime(); 
    }else{
    return dc.next() ;
    }
    }}
    /*
     * 转换11-NOV-2011格式
     */
    class Converter02 implements Converter{ final String pattern = "(\\d{2})-([A-Z]{3})-(\\d{4})";    
    final Map<String , Integer> monthMap = new HashMap<String ,Integer>();
    {
    monthMap.put("JAN", 1);
    monthMap.put("FEB", 2);
    monthMap.put("MAR", 3);
    monthMap.put("APR", 4);
    monthMap.put("MAY", 5);
    monthMap.put("JUN", 6);
    monthMap.put("JUL", 7);
    monthMap.put("AUG", 8);
    monthMap.put("SEP", 9);
    monthMap.put("OCT", 10);
    monthMap.put("NOV", 11);
    monthMap.put("DEC", 12);
    } @Override
    public Date transform(String s , ConverterManager dc ) throws Exception {
    if(s.matches(pattern)){
    String[] ss = s.replaceAll(pattern, "$1,$2,$3").split(",");
    Integer date = Integer.valueOf(ss[0]) ;
    Integer month =  monthMap.get(ss[1]) ;
    Integer year = Integer.valueOf(ss[2]) ;
    if(month == null){
    throw new Exception("月份" + ss[1] + "不存在") ;
    }
    Calendar cal = Calendar.getInstance() ;
    cal.set(year, month  - 1 , date); 
    return cal.getTime(); 
    }else{
    return dc.next() ;
    }
    }}
    添加新的转换器直接实现Converter接口,然后注册到ConverterManager就可以了,可以模仿Converter01与Converter02的实现
      

  9.   

    结果04-八月-12  -->  2012-08-04
    11-NOV-2011  -->  2011-11-11
    16-十一月-13  -->  2013-11-16
    03-OCT-2031  -->  2031-10-03
    042-十一月-12    没有找到该格式的转换器
    【转换错误】04-十九月-13    月份十九不存在
    04-十九月-13    没有找到该格式的转换器