java - 如何在Java中为日期添加2周?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23307324/
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-13 21:47:34  来源:igfitidea点击:

How do I add 2 weeks to a Date in java?

javadatetime

提问by PugsOverDrugs

I am getting a Date from the object at the point of instantiation, and for the sake of outputting I need to add 2 weeks to that date. I am wondering how I would go about adding to it and also whether or not my syntax is correct currently.

我在实例化时从对象获取日期,为了输出,我需要将 2 周添加到该日期。我想知道我将如何添加它,以及我的语法目前是否正确。

Current Java:

当前的Java:

private final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
private Date dateOfOrder;

private void setDateOfOrder()
{
    //Get current date time with Date()
    dateOfOrder = new Date();      
}

public Date getDateOfOrder()
{
    return dateOfOrder;
}

Is this syntax correct? Also, I want to make a getter that returns an estimated shipping date, which is 14 days after the date of order, I'm not sure how to add and subtract from the current date.

这个语法正确吗?另外,我想制作一个返回预计发货日期的 getter,即订单日期后 14 天,我不确定如何添加和减去当前日期。

采纳答案by Rod_Algonquin

Use Calendar and set the current time then user the add method of the calendar

使用 Calendar 并设置当前时间,然后使用日历的 add 方法

try this:

尝试这个:

int noOfDays = 14; //i.e two weeks
Calendar calendar = Calendar.getInstance();
calendar.setTime(dateOfOrder);            
calendar.add(Calendar.DAY_OF_YEAR, noOfDays);
Date date = calendar.getTime();

回答by Blue Ocean

Try this to add two weeks.

试试这个来增加两周。

long date = System.currentTimeMillis() + 14 * 24 * 3600 * 1000;
Date newDate = new Date(date);

回答by Evgeniy Dorofeev

Use Calendar

使用日历

    Date date = ...
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(Calendar.WEEK_OF_MONTH, 2);
    date = c.getTime();

回答by Alexander Kudrevatykh

If you are on java 8 you can use new date time api http://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#plusWeeks-long-

如果您使用的是 java 8,则可以使用新的日期时间 api http://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html#plusWeeks-long-

if you are on java 7 or more old version of java you should use old api http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#add-int-int-

如果您使用的是 java 7 或更高版本的 java,则应使用旧 api http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#add-int-int-

回答by loknath

if pass 14 to this addDate method it will add 14 to the current date and return

如果将 14 传递给此 addDate 方法,它将向当前日期添加 14 并返回

public String addDate(int days) throws Exception {
    final DateFormat dateFormat1 = new SimpleDateFormat(
            "yyyy/MM/dd HH:mm:ss");
    Calendar c = Calendar.getInstance();
    c.setTime(new Date()); // Now use today date.
    c.add(Calendar.DATE, addDays); // Adding 5 days
    return dateFormat1.format(c.getTime());
}

回答by Basil Bourque

Using the Joda-Time library will be easier and will handle Daylight Saving Time, other anomalies, and time zones.

使用 Joda-Time 库会更容易,并且可以处理夏令时、其他异常和时区。

java.util.Date date = new DateTime( DateTimeZone.forID( "America/Denver" ) ).plusWeeks( 2 ).withTimeAtStartOfDay().toDate();

回答by Ash_P

I will show you how we can do it in Java 8. Here you go:

我将向您展示我们如何在 Java 8 中做到这一点。 开始吧:

public class DemoDate {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();
        System.out.println("Current date: " + today);

        //add 2 week to the current date
        LocalDate next2Week = today.plus(2, ChronoUnit.WEEKS);
        System.out.println("Next week: " + next2Week);
    }
}

The output:

输出:

Current date: 2016-08-15
Next week: 2016-08-29

Java 8 rocks !!

Java 8 摇滚!!