MENU

Java Date/Time Calculation Addition and Subtraction Made Easy

TOC

Java Date and time calculations Easy addition and subtraction

 Easily with JavaCalculate addition and subtraction of date and timeThe following is a sample program that does the following
The features of the sample program are as follows

  • Returns the result of adding or subtracting a specified amount of time from the current or any date or time.
  • If a positive number is specified, time moves forward; if a negative number is specified, time moves backward.
    For example, if you want to set the date 10 days before the current date/time tocalculationThe following is a list of the cases where
    Calendar cal = addMonth(-10);
  • Any amount of time can be set for the year, month, day, hour, minute, second, and millisecond time fields.
    Also, relative to the specified timecalculationIf you want to do so, pass the Calendar class as an argument.
    For example, the following will advance one year from the specified Calendar instance.
    Calendar cal = add("specified Calendar",1,0,0,0,0,0,0);
  • If the value of each time field exceeds its range, the next larger time field is incremented or decremented.
    For example, the following will advance the current time by one hour and five minutes.
    Calendar cal = addMinute(65);


sample program


execution (e.g. program)

 In the sample program, the return value isafter calculationCalendar object that holds the date of the date, so it is converted to a java.util.Date by the java.util.Calendar#getTime() method, and then converted to a date string by java.text.SimpleDateFormat. SimpleDateFormat.

◆Example of Execution

public static void main(String[] args) {

SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println("Current date/time = "+ f.format(add(null,0,0,0,0,0,0,0,0).getTime()));
System.out.println("Date/time 1 year later = "+ f.format(addYera(1).getTime())));
System.out.println("date/time 1 month ago = "+ f.format(addMonth(-1).getTime())));
System.out.println("date/time 3 hours later = "+ f.format(addHour(3).getTime())));
System.out.println("Date/time 30 hours ago = "+ f.format(addHour(-30).getTime())));
System.out.println("Date/time 40 minutes later = "+ f.format(addMinute(40).getTime())));
System.out.println("Date/time 80 seconds later = "+ f.format(addSecond(80).getTime())));
System.out.println("Date/time from 1 month ago, 4 days later = "+ f.format(add(null,0,-1,4,0,0,0,0,0).getTime()));

}

◆Execution Result
The current date and time is set to 2008/03/25 00:00:00.

Current date/time = 2008/03/25 00:00:00
Date/Time one year from now = 2009/03/25 00:00:00
Date/Time 1 month ago = 2008/02/25 00:00:00
3 hours later date/time = 2008/03/25 03:00:00
Date/Time 30 hours ago = 2008/03/23 18:00:00
Date/Time 40 minutes later = 2008/03/25 00:40:00
Date/Time 80 seconds later = 2008/03/25 00:01:20
Date and time 4 days from 1 month ago = 2008/02/29 00:00:00

*Because 2008 is a leap year, the last day of February is the 29th.

This sample program is a Chat&Messenger "Calendar and scheduling functionsThe actual use of the system is done in the "I'm a fan of this system.

TOC