我使用的是 NetBeans IDE 8.0 测试版。
我有 2 个整数,它们的值是从数据库表中获取的。
int year, month;
try{
string sql = "select * from item where item_id = i001";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
rs.next();
year = Integer.parseInt(rs.getString("warr_years"));
month = Integer.parseInt(rs.getString("warr_months"));
}
catch(SQLException e){
}
我有一个保存当前日期的字符串变量。
DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
Date date = new Date();
String d = dateFormat.format(date);
现在我需要将年和月添加到日期中。 谁能帮我解决这个问题?
最佳答案
你需要一个 Calender
来做到这一点。
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.YEAR, year);
c.add(Calendar.MONTH, month);
Date newDate = c.getTime();
关于java - 如何将两个整数值作为年和月添加到日期变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25533363/