-3

how would i make a program that prints out the date after a number of days from June 1st,2013? Any help is appreciated

Example: if 45 is entered it should output July 16,2013

Im using ReadyToPrgram btw if this matters

3 Answers3

2

Here is one nice tutorial

SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd"); 

Calendar calendar = new GregorianCalendar(2013,7,1);    
System.out.println("Date : " + sdf.format(calendar.getTime()));

//add one month
calendar.add(Calendar.MONTH, 1);
System.out.println("Date : " + sdf.format(calendar.getTime()));
user6123723
  • 10,546
  • 18
  • 67
  • 109
  • Sorry if this is a stupid comment but im new and how do i get SimpleDateFormat? – user3183193 Jan 27 '14 at 20:52
  • @user3183193 You can use your IDE to assist you with the necessary imports. Use Eclipse or Netbeans to import. java.text is the package you need to import for SimpleDateFormat. – user6123723 Jan 27 '14 at 21:09
1

This will increase any date by exactly one

String untildate="2013-07-16";//can take any date in current format    
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );   
Calendar cal = Calendar.getInstance();    
cal.setTime( dateFormat.parse(untildate));    
cal.add( Calendar.DATE, 1 );    
String convertedDate=dateFormat.format(cal.getTime());    
System.out.println("Date increase by one.."+convertedDate);
Kick
  • 4,823
  • 3
  • 22
  • 29
1

You can use the java.util.Date and java.util.Calendar classes bundled with Java. But they are notoriously troublesome.

Joda-Time

For years now the Joda-Time third-party open-source library has been a popular substitute.

If you are sure you want just the date alone, without time and time zones use Joda-Time’s LocalDate class. Example…

LocalDate localDate = new LocalDate( 2013, 6, 1 ).plusDays( 45 );
System.out.println( "localDate " + localDate );

Search StackOverflow

Search around StackOverflow.com. These basic date and time questions have already been asked and answered.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154