在 Java 中为日期添加天数 [英] Adding days to a date in Java
本文介绍了在 Java 中为日期添加天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Java 中为日期添加 x 天?
How do I add x days to a date in Java?
例如,我的日期是 (dd/mm/yyyy) = 01/01/2012
For example, my date is (dd/mm/yyyy) = 01/01/2012
加上 5 天,输出应该是 06/01/2012
.
Adding 5 days, the output should be 06/01/2012
.
推荐答案
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Using today's date
c.add(Calendar.DATE, 5); // Adding 5 days
String output = sdf.format(c.getTime());
System.out.println(output);
这篇关于在 Java 中为日期添加天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文