Why can't my test class run? - java

so I am trying to do a test class, the instruction is
Create a class called TestDate whose constructor creates fifteen Date objects and displays the day of the week for the dates by calling each Date’s getDayOfTheWeek() method. The dates are specifically these ones, in this precise order:
but when I run it, I keep getting errors! I think it's my constructor crashing or somewhere along that line.
Below is my TestDate class code
public class TestDate
{
private Date date;
/*
* Main Constructor
* #param fifteen specific date, test to your heart's content
*/
public TestDate()
{
Date d1 = new Date(1970, 11, 15);
System.out.println(date.getDayOfTheWeek());
Date d2 = new Date(1887, 7, 31);
Date d3 = new Date(1966, 5, 2);
Date d4 = new Date(1980, 8, 19);
Date d5 = new Date(2001, 9, 11);
Date d6 = new Date(1900, 6, 26);
Date d7 = new Date(1940, 2, 28);
Date d8 = new Date(1974, 10, 30);
Date d9 = new Date(1914, 1, 15);
Date d10 = new Date(1840, 10, 1);
Date d11 = new Date(1999, 12, 31);
Date d12 = new Date(1988, 5, 20);
Date d13= new Date(2012, 3, 10);
Date d14 = new Date(2006, 4, 1);
Date d15 = new Date(1992, 2, 29);
}
}
And here is my Date Class constructor and method getDayofTheWeek, I think that's all the information related to my problem.
public class Date
{
public Date(int year, int month, int day)
{
setYear(year);
setMonth(month);
setDay(day);
}
public String getDayOfTheWeek()
{
int step0 = year%100;
int step1 = step0 / 12;
int step2 = step0 % 12;
int step3 = step2 / 4;
int step4 = day;
int step5 = 0;
if(month == JANUARY || month == OCTOBER)
{
step5 = JAN_CODE;
}
else if(month == FEBURARY || month == MARCH || month == NOVEMBER)
{
step5 = FEB_CODE;
}
else if(month == APRIL || month == JULY)
{
step5 = APR_CODE;
}
else if(month == MAY)
{
step5 = MAY_CODE;
}
else if(month == JUNE)
{
step5 = JUN_CODE;
}
else if(month == AUGUST)
{
step5 = AUG_CODE;
}
else if(month == SEPTEMBER || month == DECEMBER)
{
step5 = SEP_CODE;
}
else
{
step5 = 0;
}
if(year/100 ==16 || year/100 == 20)
{
step5 = step5 + SPECIAL_CODE_SIX;
}
else if(year/100 == 17 || year/100 == 21)
{
step5 = step5 + SPECIAL_CODE_FOUR;
}
else if(year/100 == 18)
{
step5 = step5 + SPECIAL_CODE_TWO;
}
int step6 = (step1 + step2 + step3 +step4 + step5)%MOD_CODE;
if(step6 == SAT_CODE)
{
return "Saturday";
}
else if(step6 == SUN_CODE)
{
return "Sunday";
}
else if(step6 == MON_CODE)
{
return "Monday";
}
else if(step6 == TUE_CODE)
{
return "Tuesday";
}
else if(step6 == WED_CODE)
{
return "Wednesday";
}
else if(step6 == THU_CODE)
{
return "Thursday";
}
else if(step6 == FRI_CODE)
{
return "Friday";
}
else
{
return null;
}
}
}
and I passed the parameter in the mutator method
public void setYear(int year)
{
if(year <= CURRENT_YEAR && year >= YEAR_ZERO)
{
this.year = year;
}
}
public void setMonth(int month)
{
if(month <= DECEMBER && month >= JANUARY)
{
this.month = month;
}
}
public void setDay(int day)
{
if(month == JANUARY || month == MARCH || month == MAY || month == JULY || month == AUGUST
|| month == OCTOBER || month == DECEMBER)
{
if(day <= DAY_THIRTY_ONE && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
else if(month == APRIL || month == JUNE || month == SEPTEMBER || month == NOVEMBER)
{
if(day <= DAY_THIRTY && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
else if(month == FEBURARY)
{
if((isLeapYear() == true) && day <= DAY_FEB_LEAP && day >= DAY_ONE)
{
this.day = day;
}
else if((isLeapYear() == false) && day <= DAY_FEB_NORMAL && day >= DAY_ONE)
{
this.day = day;
}
else
{
year = CURRENT_YEAR;
month = JANUARY;
day = DAY_ONE;
}
}
}
Sorry for the long list of code, I don't really know which ones are useless.

In the second line of your constructor you are calling
System.out.println(date.getDayOfTheWeek());
on the variable date which is null at this point as it has not been initialized yet. This leads to a NullPointerException. Read here for more information: What is a NullPointerException, and how do I fix it?
What I am (assuming) you wanted to do there is:
System.out.println(d1.getDayOfTheWeek());
Things besides that that I noticed:
You use the #param field in a javadoc comment without there being a parameter. Do not do this. If your constructor does not take arguments then there should not be a #param.
You are creating 15 instances of Date in your constructor whose scope end as soon as the constructor is finished. You could just delete them and nothing would change. I am assuming you want this class to be a "test" whether your dates are initialized correctly. I would recommend doing this in a main method (for a quick test while coding) and with an actual test framework like JUnit for 'real' testing. You can read this tutorial if you are interested. It also goes into detail why you actually want to test.

Related

Condtional statements using Calendar.HOUR_OF_DAY

I have this code and it has to show one of the strings based on the time of the day.
The code is
public void onTimeTick() {
mTime.setTimeInMillis(System.currentTimeMillis());
// Let's see what string we need according to the time
int saluteResId = R.string.salute_fallback;
if (mTime.get(Calendar.HOUR_OF_DAY) > 4) {
saluteResId = R.string.salute_morning;
} else if (mTime.get(Calendar.HOUR_OF_DAY) > 12) {
saluteResId = R.string.salute_evening;
} else if (mTime.get(Calendar.HOUR_OF_DAY) > 19 || mTime.get(Calendar.HOUR_OF_DAY) < 5) {
saluteResId = R.string.salute_night;
}
}
But the problem is that HOUR_OF_DAY will always remain greater than 4 so it will never even check the remaining two conditions and the string will always be set to salute_morning. I am not very good in java and trying to figure out how I can make the first condition false so it will check the other conditions and set the string according to them.
I think this can be helpful
if (mTime.get(Calendar.HOUR_OF_DAY) > 4 && mTime.get(Calendar.HOUR_OF_DAY) <= 12) {
//saluteResId = R.string.salute_morning;
} else if (mTime.get(Calendar.HOUR_OF_DAY) > 12 && mTime.get(Calendar.HOUR_OF_DAY) <= 19) {
//saluteResId = R.string.salute_evening;
} else if (mTime.get(Calendar.HOUR_OF_DAY) > 19 && mTime.get(Calendar.HOUR_OF_DAY) <= 4) {
//saluteResId = R.string.salute_night;
}
Try this code
final Calendar d = Calendar.getInstance();
final int hh = d.get(Calendar.HOUR_OF_DAY);
//try this way
String time = null;
if (hh == 0) {
time =("Salute_Night");
} else if (hh == 1) {
time =("Salute_Night");
} else if (hh == 2) {
time =("Salute_Night");
} else if (hh == 3) {
time =("Salute_Night");
} else if (hh == 4) {
time =("Salute_Night");
} else if (hh == 5) {
time =("Salute_Morning");
} else if (hh == 6) {
time =("Salute_Morning");
} else if (hh == 7) {
time =("Salute_Morning");
} else if (hh == 8) {
time =("Salute_Morning");
} else if (hh == 9) {
time =("Salute_Morning");
} else if (hh == 10) {
time =("Salute_Morning");
} else if (hh == 11) {
time =("Salute_Morning");
} else if (hh == 12) {
time =("Salute_Evening");
} else if (hh == 13) {
time =("Salute_Evening");
} else if (hh == 14) {
time =("Salute_Evening");
} else if (hh == 15) {
time =("Salute_Evening");
} else if (hh == 16) {
time =("Salute_Evening");
} else if (hh == 17) {
time =("Salute_Evening");
} else if (hh == 18) {
time =("Salute_Evening");
} else if (hh == 19) {
time =("Salute_Evening");
} else if (hh == 20) {
time =("Salute_Night");
} else if (hh == 21) {
time =("Salute_Night");
} else if (hh == 22) {
time =("Salute_Night");
} else if (hh == 23) {
time =("Salute_Night");
}
//your text view where you want it to display
textviewTimeOfDay.settext.(time);

IllegalArgumentException return the value given in object decleration

the bellow code is part of a program that is suppose to throw an IllegalArgumentException if the given value is out of range. But instead, if the given numbers in setTime() is out of range, it would return the corresponding value when the object was created in the main method instead of the desired error message! what is the reason
here is the code:
public class MyTime {
private int hour = 0;
private int minute = 0;
private int second = 0;
public static void main (String [] args) {
// when the value is out of range in setTime(), the value given bellow in t1 is returned
MyTime t1 = new MyTime (10,10,10);
t1.setTime(26, 23, 14);
System.out.println("toString(): " + t1);
}
public MyTime (int hour, int minute, int second) {
this.hour = hour;
this.minute = minute;
this.second = second;
}
public void setTime (int hour, int minute, int second) {
try {
if (hour > 0 && hour < 23 ) {
this.hour = hour;
}
if (minute > 0 && minute < 59 ) {
this.minute = minute;
}
if (second > 0 && second < 59 ) {
this.second = second;
}
}
catch (IllegalArgumentException exception) {
System.out.println("Invalid entry");
}
}
You said it should throw the exception. So you shouldn't catch it inside the method. Remove the try{...}catch{..} around the if 's
public void setTime (int hour, int minute, int second) {
if (hour > 0 && hour < 23 ) {
this.hour = hour;
} else {
throw new IllegalArgumentException();
}
if (minute > 0 && minute < 59 ) {
this.minute = minute;
} else {
throw new IllegalArgumentException();
}
if (second > 0 && second < 59 ) {
this.second = second;
} else {
throw new IllegalArgumentException();
}
}
You have to throw the exception.
public void setTime (int hour, int minute, int second) {
try {
if (hour > 0 && hour < 23 ) {
this.hour = hour;
}else{
throw new IllegalArgumentException("Invalid Hour Value");
}
if (minute > 0 && minute < 59 ) {
this.minute = minute;
}else{
throw new IllegalArgumentException("Invalid Minutes Value");
}
if (second > 0 && second < 59 ) {
this.second = second;
}else{
throw new IllegalArgumentException("Invalid Seconds Value");
}
}
catch (IllegalArgumentException exception) {
System.out.println("Invalid entry");
}
}
you need to update your code handle outside range values
eg:
if (hour > 0 && hour < 23 )
this.hour = hour;
else
throw new IllegalArgumentException();

Calling a method from another method (java)

I started learning java on my own a few weeks back and I keep running up to the same basic problem, where I can't call a method from another method in the same class. I either get a "symbol not found" error (I think because the method is out scope or the method just doesn't work anymore. The following code is part of a java exercise for creating a calender sort of a programme. I'll use the // in code commentaries to indicate, where exactly is the problem.
public class MyDate {
private int year;
private int month;
private int day;
private static String[] strMonths = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
private static String[] strDays = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
private static int[] daysInMonths = {31, 28, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static boolean isLeapYear(int year) { // This is the first method, which works fine
// when being called from the main method.
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return true;
} else {
return false;
}
};
public static boolean isLeapYear; // I put this declaration in, because I got
// "symbol not found" errors, when referencing the method from the second method.
// I'm guessing it partially invalidates the first declaration.
public static boolean isValidDate(int year, int month, int day) { // The second method
if ((year >= 1 ) && (year <= 9999)){
if ((month >= 0) && (month <= 11)) {
if ((day >= 1) && ((month == 0) || (month == 2) || (month == 4) || (month == 6)
|| (month == 7) || (month == 9) || (month == 11)) && (day <= 31)) {
return true;
}
else if ((day >= 1) && ((month == 3) || (month == 5) || (month == 8)
|| (month == 10) && (day <= 30))) {
return true;
}
else if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)){
// Code from the first method (above), which I would like to replace with just a reference
// to the first method (for instance (isLeapYear = true)),
// but it doesn't work the same as the code above (or at all).
if ((month == 1) && (day == 29)) {
return true;
}
else
if ((day >= 1) && ((month == 1) && (day <= 28))) {
return true;
}
else {
return false;
}
}
}
}
return isValidDate; }
For reference, the method isLeapYear works as it's supposed to, when testing it with this main method:
public static void main(String[] args) {
System.out.println(isLeapYear(1900)); // false
System.out.println(isLeapYear(2000)); // true
System.out.println(isLeapYear(2011)); // false
System.out.println(isLeapYear(2012)); // true
}
}
Thanks for your help!
Remove public static boolean isLeapYear; from your code and every time you need to call a function, in this case public static boolean isLeapYear(int year) { ..} you must use a parenthesis to let the compiler know that you are actually calling a function there.
In this case you should go with isLeapYear(year)
and then if (isLeapYear(year)) { .. } else {..}
Given package name of MyApplication (substitute your package name in each place this one occurs below) import isLeapYear as shown below.
package MyApplication;
public class MyDate {
public static boolean isLeapYear(int year) { // This is the first method, which works fine
// when being called from the main method.
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
return true;
} else {
return false;
}
};
}
Main class:
package MyApplication;
import static MyApplication.MyDate.isLeapYear;
public class NewClass{
public static void main(String[] args) throws Exception
{
System.out.println(isLeapYear(1900)); // false
System.out.println(isLeapYear(2000)); // true
System.out.println(isLeapYear(2011)); // false
System.out.println(isLeapYear(2012));
}
}

Javabat Help: alarmClock

I am working through the JavaBat questions and am confused about my logic.
Here's the task:
Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and
a boolean indicating if we are on vacation, return a string of the
form "7:00" indicating when the alarm clock should ring. Weekdays, the
alarm should be "7:00" and on the weekend it should be "10:00". Unless
we are on vacation -- then on weekdays it should be "10:00" and
weekends it should be "off".
alarmClock(1, false) → "7:00" alarmClock(5, false) → "7:00"
alarmClock(0, false) → "10:00"
Here's my code:
public String alarmClock(int day, boolean vacation) {
if ( (day >=1 && day <=5) && (!vacation)) {
return "7:00";
} else if ( (day >=1 && day <=5) && (vacation)) {
return "10:00";
} else {
return "off";
}
}
Why do these two tests fail?
alarmClock(0, false) → "10:00" "off" X
alarmClock(6, false) → "10:00" "off" X
Surely, this line covers it?
if (day >=1 && day <=5) && (!vacation))
how about this?
public String alarmClock(int day, boolean vacation) {
if (day >=1 && day <=5) {
return vacation ? "10:00" : "7:00";
} else {
return vacation ? "off" : "10:00";
}
}
Note it does depend if your coding convention allows the use of the turnary operator. But in this case I think the logic is easier to read.
Surely, this line covers it?
if ((day >=1 && day <=5) && (!vacation))
No, that line doesn't cover it. If the day is Sunday or Saturday (0 or 6), the first part of your "and" expression (day >=1 && day <=5) will be false, since 0 and 6 are not between 1 and 5 inclusive.
The only branch that handles days 0 and 6 is your else branch: "off".
This is a great time to use helper methods to express your logic closer to the English description:
if ( isWeekday(day) ) {
if ( vacation ) {
//what to return here?
} else {
//what to return here?
}
} else {
if ( vacation ) {
//what to return here?
} else {
//what to return here?
}
}
Then you just need to implement isWeekday:
private boolean isWeekday(int day) {
return /*fill this in*/;
}
Although the ternary operator is useful here, as this problem can be done in a single return statement, the following code below allows readability.
public String alarmClock(int day, boolean vacation) {
if(vacation){ //if we are on vacation
if(day > 0 && day < 6){ //if it is weekday and we are on vacation
return "10:00";
}
else return "off"; //it must be the weekend!
}
//from here on out all the cases where vacation is true have been weeded out
if(day > 0 && day < 6){
return "7:00";
}
else return "10:00";
}
if ((day == 0 || day == 6) && (!vacation) || (day >= 1 && day <= 5) && (vacation)) {
return "10:00";
}
if ((day >= 1 && day <= 5) && (!vacation)) {
return "7:00";
}
return "off";
public String alarmClock(int day, boolean vacation) {
if (((day==0)||(day==6))&&(!vacation)){
return "10:00";
}
else if (((day!=0)||(day!=6))&&(!vacation)){
return "7:00";
}
else if (((day==0)||(day==6))&&(vacation)){
return "off";
}
else{
return "10:00";
}
}

Days Between Dates Java (homework)

This is a couple days off when I run the program. Any advice on what I am doing wrong?
I know there is a simpler way to do it, but for this I'm trying to show all the actual steps in finding the days between.
Homework assignment, so cannot use date-time libraries.
public class DaysBetween {
public static void main (String []args) {
long months1 = Long.parseLong(args[0]);
long days1 = Long.parseLong(args[1]);
long year1 = Long.parseLong(args[2]);
long months2 = Long.parseLong(args[3]);
long days2 = Long.parseLong(args[4]);
long year2 = Long.parseLong(args[5]);
long daysbetween = 0;
long leapyearcounter = 0;
boolean leapyear1 = false;
boolean leapyear2 = false;
boolean valid1 = true;
boolean valid2 = true;
int earlier = 0;
// this tests to see which date is earlier
if (year1 == year2){
if (months1 == months2) {
if (days1 == days2) {
daysbetween = daysbetween;
} else if (days1 < days2) {
earlier = 1;
}
} else if (months1 < months2) {
earlier = 1;
}
} else if (year1 < year2 ) {
earlier = 1;
}
// this switches the dates depending on which is earlier
switch(earlier) {
case 1: months1 = Long.parseLong(args[0]);
days1 = Long.parseLong(args[1]);
year1 = Long.parseLong(args[2]);
months2 = Long.parseLong(args[3]);
days2 = Long.parseLong(args[4]);
year2 = Long.parseLong(args[5]);
break;
default:
months1 = Long.parseLong(args[3]);
days1 = Long.parseLong(args[4]);
year1 = Long.parseLong(args[5]);
months2 = Long.parseLong(args[0]);
days2 = Long.parseLong(args[1]);
year2 = Long.parseLong(args[2]);
break;
}
//this section tests if the earlier date is valid
if((year1 % 4 == 0) && (year1 % 100 != 0) || (year1 % 400 == 0)) {
leapyear1 = true;
}
if ((leapyear1 == true) && (months1 == 2)) {
if (days1 <= 29 && days1 >=1){
valid1 = true;
}
} else if ((leapyear1 == false) && (months1 == 2)){
if (days1 <= 28 && days1 >= 1){
valid1 = true;
}
}
if (months1 != 2) {
if (months1 < 8 ) {
if (months1 %2 != 0) {
if (days1 <= 31 && days1 >=1){
valid1 = true;
} else {
valid1 = false;
}
} else if (months1 %2 == 0) {
if (days1 <= 30 && days1 >=1){
valid1 = true;
} else {
valid1 = false;
}
}
} else if (months1 > 8) {
if (months1 %2 != 0) {
if (days1 <= 30 && days1 >=1){
valid1 = true;
} else {
valid1 = false;
}
} else if (months1 %2 == 0) {
if (days1 <= 31 && days1 >=1){
valid1 = true;
} else {
valid1 = false;
}
}
}
}
// this section tests if the later date is valid
if((year2 % 4 == 0) && (year2 % 100 != 0) || (year2 % 400 == 0)) {
leapyear2 = true;
}
if ((leapyear2 == true) && (months2 == 2)) {
if (days2 <= 29 && days2 >=1){
valid2 = true;
}
} else if ((leapyear2 == false) && (months2 == 2)){
if (days2 <= 28 && days2 >= 1){
valid2 = true;
}
}
if (months2 != 2) {
if (months2 < 8 ) {
if (months2 %2 != 0) {
if (days2 <= 31 && days2 >=1){
valid2 = true;
} else {
valid2 = false;
}
} else if (months2 %2 == 0) {
if (days2 <= 30 && days2 >=1){
valid2 = true;
} else {
valid2 = false;
}
}
} else if (months2 > 8) {
if (months2 %2 != 0) {
if (days2 <= 30 && days2 >=1){
valid2 = true;
} else {
valid2 = false;
}
} else if (months2 %2 == 0) {
if (days2 <= 31 && days2 >=1){
valid2 = true;
} else {
valid2 = false;
}
}
}
}
//this adds a day to the total if the earlier date is in january & its a leap year
if ((months1 == 1) && (leapyear1 == true)) {
daysbetween = daysbetween +1;
}
//this adds the months left in year1
if (months1 == 1) {
daysbetween = daysbetween + 334;
} else if (months1 == 2) {
daysbetween = daysbetween + 306;
} else if (months1 == 3) {
daysbetween = daysbetween + 275;
} else if (months1 == 4) {
daysbetween = daysbetween + 245;
} else if (months1 == 5) {
daysbetween = daysbetween + 214;
} else if (months1 == 6) {
daysbetween = daysbetween + 184;
} else if (months1 == 7) {
daysbetween = daysbetween + 153;
} else if (months1 == 8) {
daysbetween = daysbetween + 122;
} else if (months1 == 9) {
daysbetween = daysbetween + 92;
} else if (months1 == 10) {
daysbetween = daysbetween + 61;
} else if (months1 == 11) {
daysbetween = daysbetween + 31;
} else { }
// this adds the extra day if year2 is leap year and if the month is march or later
if ((months2 >= 3) && (leapyear2 = true)) {
daysbetween = daysbetween + 1;
}
// this adds the months up to the month in year2
if (months2 == 1) {
daysbetween = daysbetween;
} else if (months2 == 2) {
daysbetween = daysbetween + 31;
} else if (months2 == 3) {
daysbetween = daysbetween + 59;
} else if (months2 == 4) {
daysbetween = daysbetween + 90;
} else if (months2 == 5) {
daysbetween = daysbetween + 120;
} else if (months2 == 6) {
daysbetween = daysbetween + 151;
} else if (months2 == 7) {
daysbetween = daysbetween + 181;
} else if (months2 == 8) {
daysbetween = daysbetween + 212;
} else if (months2 == 9) {
daysbetween = daysbetween + 243;
} else if (months2 == 10) {
daysbetween = daysbetween + 273;
} else if (months2 == 11) {
daysbetween = daysbetween + 304;
} else if (months2 == 12) {
daysbetween = daysbetween + 334;
} else { }
//this add the days left in month1
if (months1 != 2) {
if (months1 < 8 ) {
if (months1 %2 != 0) {
daysbetween = daysbetween + (31 - days1);
} else if (months1 %2 == 0) {
daysbetween = daysbetween + (30 - days1);
}
} else if (months1 > 8) {
if (months1 %2 != 0) {
daysbetween = daysbetween + (30 - days1);
} else if (months1 %2 == 0) {
daysbetween = daysbetween + (31 - days1);
}
}
}
// this add the days left in month1 if its feb
if ((leapyear1 == true) && (months1 == 2)) {
daysbetween = daysbetween + (29 - days1);
} else {
daysbetween = daysbetween + (28 - days1);
}
daysbetween = daysbetween + days2; // adds the days left in month2
daysbetween = daysbetween + ((((year2-1)-(year1+1))+1)*365); // adds the days in the years to the total
for (long i = (year1+1); i < (year2-1) ; i++) { // sees the # of leapyears b/n year1 & year2
if ((i % 4 == 0) && (i % 100 != 0) || (i % 400 == 0)) {
leapyearcounter++;
}
}
daysbetween = daysbetween + leapyearcounter; // adds the leapyear days
if ((valid1 == false) || (valid2 == false)) {
System.out.println("Sorry! You gave me one or more invalid dates!");
} else if (daysbetween == 1){
System.out.println("There is " + daysbetween + " day between those two dates!");
} else if (daysbetween != 1) {
System.out.println("There are " + daysbetween + " days between those two dates!");
} else if (daysbetween == 0) {
System.out.println("There are no days between those two dates!");
}
}
}
Unless this homework or just some self hurting thing you have going can I recommend using the joda time library for all your datetime calculations:
Time to use joda answer: ~22 minutes
There's a lot that needs to be said here, but I'd recommend that you start by refactoring this monster into lots of smaller methods that you can test individually. Your code is difficult to read, understand, and debug because of the sheer volume. "Decomposition" is the operative word.
Your main method should only have driver or test code in it.
You should have methods like boolean isValid(Date date) and boolean before(Date d1, Date d2) and boolean isLeapYear(int year). Test these individually and start building up to what you want.
Use java.util.Date: (date1.getTime()-date2.getTime())/(1000*60*60*24).

Categories