w3resource logo


Java Programming Exercies

Java DateTime, Calendar Exercises: Get a date after 2 weeks

Secondary Nav

Java DateTime, Calendar : Exercise-16 with Solution

Write a Java program to get a date after 2 weeks.

Java Code:

import java.util.*;
public class Exercise16 {
   public static void main(String[] args)
    {
     //two weeks
     int noOfDays = 14; 
     Calendar cal = Calendar.getInstance();
     Date cdate = cal.getTime();
     cal.add(Calendar.DAY_OF_YEAR, noOfDays);
     Date date = cal.getTime();
     System.out.println("\nCurrent Date: " + cdate+"\n");  
     System.out.println("Day after two weeks: " + date+"\n");  	
    }
}

Sample Output :

Current Date: Fri Sep 16 13:12:02 IST 2016                              
                                                                        
Day after two weeks: Fri Sep 30 13:12:02 IST 2016 

Java Practice online:

import java.util.*;
public class Exercise16 {
   public static void main(String[] args)
    {
     //two weeks
     int noOfDays = 14; 
     Calendar cal = Calendar.getInstance();
     Date cdate = cal.getTime();
     cal.add(Calendar.DAY_OF_YEAR, noOfDays);
     Date date = cal.getTime();
     System.out.println("\nCurrent Date: " + cdate+"\n");  
     System.out.println("Day after two weeks: " + date+"\n");  	
    }
}



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables