This Java program demonstrates how to decrement a date by a specified number of months using the Calendar class. The program first creates a new instance of Calendar using the getInstance() method, which returns a calendar object set to the current date and time. The program then prints the current date and time using the getTime() method of the Calendar class.
To decrement the date by three months, the program calls the add() method of the Calendar class, passing in the Calendar.MONTH field and a negative value of three. This subtracts three months from the current date and time stored in the Calendar object. Finally, the program prints the updated date and time using the getTime() method of the Calendar class.
Overall, this program provides a simple example of how to decrement a date by a specified number of months using the Calendar class in Java. However, it is worth noting that the Calendar class has been replaced by the newer java.time API in Java 8 and later versions, which provides more flexible and robust date and time handling capabilities.
import java.util.Calendar; class Decrement_Month { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); System.out.println("Current Date : " + cal.getTime()); cal.add(Calendar.MONTH, -3); System.out.println("Updated Date (-3 Months) : " + cal.getTime()); } }
Current Date : Sat Nov 05 14:36:39 IST 2022 Updated Date (-3 Months) : Fri Aug 05 14:36:39 IST 2022
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions