java中 Date取具体年份year

来源:互联网 发布:ubuntu 安装命令 编辑:程序博客网 时间:2024/06/05 03:18

由于getYear()方法不支持所以使用以下方法

方法一:使用Calendar 

       实例:

Date d = rs.getDate("date");//方法一   一般为Date d = new Date();Calendar c = Calendar.getInstance();c.setTime(d);int y = c.get(Calendar.YEAR);
方法二:使用SimpleDateFormat

       实例:

String test=new SimpleDateFormat("yyyy").format(rs.getDate("date"));//方法二   一般为.format(new Date())int y = Integer.parseInt(test);

附:1.类型转换方法(String转int)https://zhidao.baidu.com/question/290265469.html
        2.SimpleDateFormat使用详解  http://blog.csdn.net/gubaohua/article/details/575488
        3.在MySql中根据年份查询数据表中的数据https://zhidao.baidu.com/question/302206563.html

        4.数组、List、ArrayList用法区别http://www.cnblogs.com/a164266729/p/4561651.html

0 0