2013-08-12 13 views
7

Jestem nowy na tej stronie i dopiero zacząłem uczyć się Javy. Próbuję dodać kilka dni do kalendarza gregoriańskiego, ale to nie działa. Tutaj ... (zignoruj ​​górny fragment), a denerwujące są daty dodawania na dole.Dodawanie dni do kalendarza

/* 
* Author:Matt M 
* Date:8.12.13 
* Discription: When the user inputs the deadline, and the difficulity of the project, 
* the program gives the date he should start working on it 
*/ 
import java.util.*; 
public class DeadlinePlanner{ 
    public static void main(String[] args) 
    { 
     //take information and restart questions if information is wrong 
     int month = 0, day = 0 ; 
     do 
     { 
     do 
     { 
     System.out.println("Input the month please"); 
     month = (new Scanner(System.in).nextInt() - 1); 
     System.out.println("Input the day please"); 
     day = (new Scanner(System.in).nextInt()); 

     } 
     while (!(month <= 12) || !(month >= 0)); 
     } 
     while (!(day <= 31) || !(month >= 0)); 

     //Make new calender and initialize it 
     GregorianCalendar setup = new GregorianCalendar(); 
     setup.set(2013, month, day); 
     System.out.println("The deadline is "+ setup.getTime()); 

     //switch statement to give starting date 
     System.out.println("Is the project hard or easy?"); 
     Scanner difficulity = new Scanner(System.in); 

     switch (difficulity.nextLine()) 
     { 

      case "easy": 


       setup.add(day, -1); 
       System.out.print("The date you should start workinng on is "); 
       System.out.println(setup.getTime()); 
       break; 
      case "hard": 

       setup.add(day, -10); 
       System.out.print("The date you should start workinng on is "); 
       System.out.println(setup.getTime()); 

       break; 
      default: 
       System.out.println("Your answers to the questions are incorrect"); 
       break; 
     } 

    } 
} 

Dzięki za przeczytanie tego! Jestem otwarty na wszelkie opinie ...

Odpowiedz

18

Jest zbyt dużo kod tutaj. Za dużo interakcji użytkownika.

Zacznij od prostej metody, aby zrobić jedną rzecz, a następnie rozwiń się po tym, jak to zrobisz.

Oto jak można to zrobić:

public class DateUtils { 
    private DateUtils() {} 

    public static Date addDays(Date baseDate, int daysToAdd) { 
     Calendar calendar = Calendar.getInstance(); 
     calendar.setTime(baseDate); 
     calendar.add(Calendar.DAY_OF_YEAR, daysToAdd); 
     return calendar.getTime(); 
    } 
} 

Po przetestowaniu tej metody i sprawdzone można pozwolić Reszta kodu po prostu nazwać.

AKTUALIZACJA: To cztery lata później, a JDK 8 dał nam nowy pakiet czasu oparty na JODA. Powinieneś używać tych klas, nie z JDK 1.0 Calendar.

+0

Dziękuję bardzo, twoja odpowiedź była bardzo pomocna – user2676580

+0

Cieszę się. Powodzenia we wspinaniu się na krzywą nauki języka Java. – duffymo

+0

Czy to możliwe, że używanie wszystkich "DAY_OF_X" jest ok? –

1

Musisz zmienić linie, które wyglądają jak:

setup.add(day, -1); 
setup.add(day, -10); 

do

setup.add(GregorianCalendar.DAY_OF_MONTH, -1); 
setup.add(GregorianCalendar.DAY_OF_MONTH, -10); 

Zobacz GregorianCalendar aby uzyskać więcej informacji.

1

gregoriański kalandra ma swoją wartość należy używać, aby poinformować go, czego rosną gdzie mówisz

setup.add(day, -1); 

należy użyć gregoriański wartość Calander na dzień

setup.add(Calendar.DAY_OF_MONTH, -1); 
0
Calendar c = new GregorianCalendar(2000, Calendar.JANUARY, 1); 
c.add(Calendar.DAY_OF_MONTH, 1); 

Więcej informacji o Kalendarzu i jego polach można znaleźć tutaj Calendar

Spróbuj również przejrzeć ten post: here