我要将日期类型2008-04-14 12:03:45,转换成日期类型 Date 2008,怎么转呢?
以下方法的结果是:Tue Jan 01 00:00:00 CST 2008,但是我这只想要2008,动态显示在页面上的,其他的不要啊,请各位大侠看看
logouttime是从数据库中查出来的日期类型的数据,格式是:2008-04-14 12:03:45,DateFormat df1 = new SimpleDateFormat("yyyy");
Date year = df1.parse(df1.format(logouttime));

解决方案 »

  1.   

    Date是日期,怎么能只有年呢?df1.format(logouttime) 这个就是年份的字符串。
      

  2.   

    如果返回的是String类型可也这样实现
    String strDate = "2008-04-14 12:03:45";
    SimpleDateFormat format = new SimpleDateFormat("yyyy");
    try{
    System.out.println(format.format((format.parse(strDate))));
    }catch(ParseException e){
    e.printStackTrace();
    }
    如果是Date型直接format就行了
      

  3.   

    要解析的是日期类型的 Date 2008-04-14 12:03:45 ,不是字符串类型的
    期望的结果也是日期类型的,不是字符串类型的
    看来,好像真没办法