-3

print out today's date. this should be in the form MM/DD/YYYY. Months should start for 1 instead of 0. prompt to read number of days to be added to current date and print the new date.

please anyone help me out

    import java.util.calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;

    public class JavaDateAdd {

     public static void main(String[] args) {
    Date date = new Date();

    System.out.println("Today's Date Is: " + (now.get(Calendar.MONTH) + 1) +         "/" + now.get(Calendar.DATE) + "/" + now.get(Calendar.YEAR));

    System.out.print("Number of Days You Want To ADD: ");
    int AddDays = in.nextInt();

    Date newDate = addDays(date,AddDays);
        System.out.println("Java Date after adding "+AddDays+" days: "+(now.get(Calendar.MONTH) + 1) + "/" + now.get(Calendar.DATE) + "/" + now.get(Calendar.YEAR));


     }
    }`
nubteens
  • 5,462
  • 4
  • 20
  • 31
  • 3
    Have a look at `SimpleDateFormat`. Adding days [example](http://stackoverflow.com/questions/21842934/how-to-add-days-to-java-simple-date-format/21842959#21842959), [example](http://stackoverflow.com/questions/9690364/how-to-add-number-of-days-to-the-date-given-in-a-jtextfield-with-string-data-typ/28121539#28121539), [example](http://stackoverflow.com/questions/31716991/how-to-get-dates-for-next-seven-days-from-calendar-java/31717081#31717081) – MadProgrammer Sep 24 '15 at 05:01
  • i am very new to java and i am not able to get anything bro. – Mohammad Bobat Sep 24 '15 at 05:04
  • 2
    I'm voting to close this question as off-topic because: "[Questions asking for homework help must include a **summary of the work you've done so far to solve the problem**, and a **description of the difficulty you are having solving it**.](http://stackoverflow.com/help/on-topic)" – Pshemo Sep 24 '15 at 05:05
  • 1
    @MohammadBobat Then you need to go back to your instructor and ask for clarification, if you're having trouble understanding what is been asked of you, there is very little that any one here can do that will help. Once you've made some attempts and if your stuck on a particular issue, then don't hesitate to post a question, providing your attempts – MadProgrammer Sep 24 '15 at 05:07
  • @MadProgrammer i have wrote the code this much ... you can see in the question i just edited.. can you please help me further? – Mohammad Bobat Sep 24 '15 at 05:25
  • @Aiden if i want user to input how many he wants to add? – Mohammad Bobat Sep 24 '15 at 05:39
  • @MohammadBobat *"if i want user to input how many he wants to add?"* - Have a look at [Scanning](https://docs.oracle.com/javase/tutorial/essential/io/scanning.html) – MadProgrammer Sep 24 '15 at 05:42

3 Answers3

0

Date Formatting using SimpleDateFormat: SimpleDateFormat is a concrete class for formatting and parsing dates in a locale-sensitive manner. SimpleDateFormat allows you to start by choosing any user-defined patterns for date-time formatting. For example:

import java.util.*;
import java.text.*;

public class HelloWorld {
public static void main(String args[]) {

    Date dNow = new Date( );
    SimpleDateFormat sdf = 
    new SimpleDateFormat ("MM/dd/yyyy ");
    System.out.println("Current Date: " + sdf.format(dNow));

    Calendar c = Calendar.getInstance();
    c.setTime(new Date()); // Now use today date.
    c.add(Calendar.DATE, 35); // Adding 35 days
    String output = sdf.format(c.getTime());
    System.out.println("New Date: "+ output);
 }
}

This would produce the following result:

Current Date: 09/24/2015                                                                                                                               
New Date: 10/29/2015 
Shekhu
  • 1,998
  • 5
  • 23
  • 49
0

You can modify the code to accept take no of days and to accept your date format.

public static void main(String[] args) {

               Calendar cal = Calendar.getInstance();
               // print current date
               System.out.println("The current date is : " + cal.getTime());
               // add 20 days to the calendar
               cal.add(Calendar.DATE, 20);
               System.out.println("20 days later: " + cal.getTime());
               Date tommrrow = cal.getTime();
               SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy");
               String date = formatter.format(tommrrow);
               System.out.println("20 days in dd-MM-yy: " + date);

}
Darcy
  • 1
  • 3
0

print out today's date. this should be in the form MM/DD/YYYY. Months should start for 1 instead of 0.

Start by taking a look at DateTimeFormatter, for example:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
System.out.println("Today is " + formatter.format(LocalDateTime.now()));

prompt to read number of days to be added to current date

Start by having a look at Scanning, for example:

    Scanner scanner = new Scanner(System.in);
    System.out.print("How many days to add: ");
    int days = scanner.nextInt();

and print the new date.

Start by having a look at Java 8's Date Time API, for example:

LocalDateTime ldt = LocalDateTime.now();
ldt = ldt.plusDays(days);
System.out.println(formatter.format(ldt));
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366