how to calculate 30 days from a date in java

Solutions on MaxInterview for how to calculate 30 days from a date in java by the best coders in the world

showing results for - "how to calculate 30 days from a date in java"
Ornella
14 Jul 2018
1package com.mkyong.time;
2
3import java.text.DateFormat;
4import java.text.SimpleDateFormat;
5import java.util.Calendar;
6import java.util.Date;
7
8public class DateExample {
9
10    private static final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
11
12    public static void main(String[] args) {
13
14        Date currentDate = new Date();
15        System.out.println(dateFormat.format(currentDate));
16
17        // convert date to calendar
18        Calendar c = Calendar.getInstance();
19        c.setTime(currentDate);
20
21        // manipulate date
22        c.add(Calendar.YEAR, 1);
23        c.add(Calendar.MONTH, 1);
24        c.add(Calendar.DATE, 1); //same with c.add(Calendar.DAY_OF_MONTH, 1);
25        c.add(Calendar.HOUR, 1);
26        c.add(Calendar.MINUTE, 1);
27        c.add(Calendar.SECOND, 1);
28
29        // convert calendar to date
30        Date currentDatePlusOne = c.getTime();
31
32        System.out.println(dateFormat.format(currentDatePlusOne));
33
34    }
35
36}