Java 计算未来日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18733582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:34:53  来源:igfitidea点击:

Calculating a future date

javadatedatetime

提问by Shehan.W

I have created a program to calculate days and in my program i want add 7 more days to the current date.

我创建了一个程序来计算天数,在我的程序中,我想在当前日期上再添加 7 天。

Which means if today's date is 9/11/2013, i want to make it 9/18/2013 by getting the current date and adding 7 more days to it. How do i implement this.

这意味着如果今天的日期是 2013 年 9 月 11 日,我想通过获取当前日期并再添加 7 天来使其成为 2013 年 9 月 18 日。我如何实现这一点。

I know how to get today's date by using the date class but i don't know to add another 7 days to the current date.

我知道如何使用 date 类获取今天的日期,但我不知道在当前日期上再添加 7 天。

This is the method i used to get the current date :

这是我用来获取当前日期的方法:

 public void dateCalculator(){
    Date date;
    date=new Date();
           }

Thank you for your time.

感谢您的时间。

采纳答案by MansoorShaikh

Calendar c = Calendar.getInstance();

c.setTime(new Date()); // Now use today date.

c.add(Calendar.DATE, 15); // Adds 15 days

回答by Subhrajyoti Majumder

you can get that by Calendar#add(Calender.DATE,7)

你可以通过 Calendar#add(Calender.DATE,7)

code snippet -

代码片段——

Calendar cal = Calendar.getInstance();
System.out.println("current date: " + cal.getTime());
cal.add(Calendar.DATE, 7);
System.out.println("7 days later: " + cal.getTime());

result -

结果 -

current date: Tue Sep 10 15:53:17 MST 2013
7 days later: Tue Sep 17 15:53:17 MST 2013

Note: code compiled in - http://www.compileonline.com/compile_java_online.php

注意:代码编译在 - http://www.compileonline.com/compile_java_online.php