Add n number of days to given date

JavaGalaxy.com 0 Tallied Votes 1K Views Share

Add n number of days to given date

public static void main(String args[]) throws Exception {
     SetTest st = new SetTest();
     st.addDaysToDate("11/11/2007", 2);
}

    private void addDaysToDate(String date, int daysToAdd) throws Exception {
     Date todayDate = new Date();
     DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
     String strDate = sdf.format(todayDate);
     Date parsedDate = sdf.parse(date);
     System.out.println(strDate);
     System.out.println(parsedDate);
    
     Calendar now = Calendar.getInstance();
     now.setTime(parsedDate); 
     now.add(Calendar.DAY_OF_MONTH, daysToAdd);
     System.out.println(now.getTime());
}
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster

I would have suggested for the user to give the date format as well. Or perhaps a try-catch with a throw inside the catch in case the user didn't enter a valid date.

frodo_cute21 0 Newbie Poster

hi! can you help me with my java programming?? plssssssssssssssssssss!!! im new to the java. here's my problem..
pls do solve my problem.. thanks!!!
hope to hear from you soon???
pls make the code.. god bless!!!

Activity 2

Place all the files in a folder named ClassList.
1. Design a class named Date with the following attributes: month, date and year which is all of type integer.
2. Design a class named Person with the following attributes: name and address both of type String and birthday of type Date.
3. Design a class named Student which is a Person with the following additional attributes: id and year both of type integer and school and course both of type String.
4. Write a class named ClassList which contains an array of Student with the maximum capacity of 100. It should also have the following method:
public boolean add(Student s)
-adds the student to the array
public boolean edit(Student s)
-edit the student’s entries in the array
public boolean delete(int id)
- delete the student with the matching id number in the array
public Student search(int id)
-search the student record in the array
public void display()
- shows all the records of the student in the array
5. Design a GUI for the class list that would allow the add, edit, delete, search and display of the students.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.