Java

How to add or subtract time from Date in Java

Sample code to add 30 seconds to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.SECOND, 30);
        Date addThirtySeconds = cal.getTime();
        System.out.println("New Date and Time: " + addThirtySeconds);

Sample code to subtract 30 seconds from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.SECOND, -30);
        Date thirtySecondsBack = cal.getTime();
        System.out.println("New Date and Time: " + thirtySecondsBack);

Sample code to add 2 minutes to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MINUTE, 2);
        Date addTwoMinutes = cal.getTime();
        System.out.println("New Date and Time: " + addTwoMinutes);

Sample code to subtract 2 minutes from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MINUTE, -2);
        Date twoMinutesBack = cal.getTime();
        System.out.println("New Date and Time: " + twoMinutesBack);

Sample code to add an hour to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.HOUR, 1);
        Date addAnHour = cal.getTime();
        System.out.println("New Date and Time: " + addAnHour);

Sample code to subtract an hour from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.HOUR, -1);
        Date anHourBack = cal.getTime();
        System.out.println("New Date and Time: " + anHourBack);

Sample code to add 3 days to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.DATE, 3);
        Date addThreeDays = cal.getTime();
        System.out.println("New Date and Time: " + addThreeDays);

Sample code to subtract 3 days from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.DATE, -3);
        Date threeDaysBack = cal.getTime();
        System.out.println("New Date and Time: " + threeDaysBack);

Sample code to add 2 months to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MONTH, 2);
        Date addTwoMonths = cal.getTime();
        System.out.println("New Date and Time: " + addTwoMonths);

Sample code to subtract 2 months from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.MONTH, -2);
        Date twoMonthsBack = cal.getTime();
        System.out.println("New Date and Time: " + twoMonthsBack);

Sample code to add 3 years to Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.YEAR, 3);
        Date addThreeYears = cal.getTime();
        System.out.println("New Date and Time: " + addThreeYears);

Sample code to subtract 3 years from Date in Java

        Date now = new Date();
        System.out.println("Current Date and Time: " + now);
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        cal.add(Calendar.YEAR, -3);
        Date threeYearsBack = cal.getTime();
        System.out.println("New Date and Time: " + threeYearsBack);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s