由于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.youkuaiyun.com/gubaohua/article/details/575488
3.在MySql中根据年份查询数据表中的数据 https://zhidao.baidu.com/question/302206563.html
4.数组、List、ArrayList用法区别http://www.cnblogs.com/a164266729/p/4561651.html

本文介绍了在Java中获取日期年份的两种方法:一是使用Calendar类,二是使用SimpleDateFormat类。这两种方法都能有效从Date对象中提取年份信息,并提供了具体的实现代码示例。
1万+

被折叠的 条评论
为什么被折叠?



