如何在Java中将修改日期提前6个月
发布时间:2020-12-15 05:00:52 所属栏目:Java 来源:网络整理
导读:参见英文答案 Java: Customize adding 1 month to the current date????????????????????????????????????3个 我希望在Java中增加6个月,目前我在代码下面使用.但它总是在第一个月印刷.你能告诉我这里的错误吗?我是Java的初学者. 这是我的输出: 现行日期:2
参见英文答案 >
Java: Customize adding 1 month to the current date????????????????????????????????????3个
我希望在Java中增加6个月,目前我在代码下面使用.但它总是在第一个月印刷.你能告诉我这里的错误吗?我是Java的初学者. 这是我的输出: 现行日期:2013年1月11日 6个月后的日期:2013年7月11日 预期产量: 当前日期:2013年5月11日 6个月后的日期:2013年11月11日 String dt = "11-05-2013"; DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy"); Date date = null; try { date = (Date)formatter.parse(dt); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Calendar now = Calendar.getInstance(); now.setTime(date); System.out.println("Current date : " + now.get(Calendar.DATE)+ "-" +(now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.YEAR)); now.add(Calendar.MONTH,6); System.out.println("date after 6 months : " + now.get(Calendar.DATE)+"-" + (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.YEAR)); 解决方法
尝试使用大写M:
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |