java中 Date取具体年份year

由于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

猜你喜欢

转载自blog.csdn.net/liuhuoer/article/details/69397156