Write a Java program to get a date after 2 weeks


This Java program calculates the date that is two weeks after the current date. Here is a breakdown of the code:

  • The program starts by importing the java.util.* package, which includes classes for working with dates and times.
  • The program then defines a class called DateAfter_TwoWeeks.
  • Inside the class, a variable nod is defined and assigned the value of 14, which represents the number of days in two weeks.
  • The program then creates a new instance of the Calendar class by calling the getInstance() method.
  • The current date is obtained by calling the getTime() method on the calendar object and assigning the resulting Date object to a variable called cd.
  • The add() method is called on the calendar object with Calendar.DAY_OF_YEAR and nod as arguments to add two weeks to the current date.
  • The resulting Date object is assigned to a variable called d.
  • Finally, the current date and the date that is two weeks after the current date are printed to the console using the println() method.

Note that the java.util.Calendar class is an older way of working with dates and times in Java. Starting from Java 8, it is recommended to use the java.time package, which provides a more modern and comprehensive set of classes for working with dates and times.

Source Code

import java.util.*;
public class DateAfter_TwoWeeks
{
	public static void main(String[] args)
	{
		int nod = 14; 
		Calendar cal = Calendar.getInstance();
		Date cd = cal.getTime();
		cal.add(Calendar.DAY_OF_YEAR, nod);
		Date d = cal.getTime();
		System.out.println("Current Date : " + cd);  
		System.out.println("Day After Two Weeks : " + d);  	
	}
}

Output

Current Date : Sat Nov 05 13:43:27 IST 2022
Day After Two Weeks : Sat Nov 19 13:43:27 IST 2022

Example Programs