简体   繁体   中英

How to get date which is the sum of any previous date to 7 days next date?

I've a previous date from current date which is saved in database and need to get date 7 days next date. How can i get it?

For example:

i've date 1461560032085 milliseconds. How can i get 7 days next date?

1 day = 86400000 milliseconds

So 7 days after "1461560032085" will be = 1461560032085 + 86400000 * 7

Hope this helps!

It is very simple to use Calendar class

Calendar calendar = Calendar.getInstance();
calendar.setTime(your_current_date);
calendar.add(Calendar.DAY_OF_YEAR, +7);
Date newDate = calendar.getTime();
 public static String getAdded_date(String previous_date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(sdf.parse(previous_date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.add(Calendar.DAY_OF_WEEK, 7);  // number of days to add, can also use Calendar.DAY_OF_MONTH in place of Calendar.DATE
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String output = sdf1.format(c.getTime());

        return output;
    }
  private int getaddedDate(int previousdate)
    {
      return previousdate + TimeUnit.DAYS.toMillis(7);
    }

要计算当天的7天后,您应该执行以下操作:

nextWeek = yourdate + 7*24*60*60*1000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM