Creating a date object.
I want to determine a date from its integer representation. This is what I have so far but when I run this program the output is always January 0, 1900, no matter what values I enter for Month, Year and day. The program is designed to start from January 1, 1900. help.
import java.awt.* ;
import javax.swing.JOptionPane ;
class Date{
staticfinalint MMDDYY= 1 ;
staticfinalint MMMYYYY= 2 ;
staticfinalint MMMMDDYYYY= 3 ;
privateint[]daysInYear={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,} ;
privateint[]daysInLeapYear ={ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,} ;
privateintintegerDate ;
private Boolean isLeapYear(int year){
return ( ( ( (year % 4) == 0 ) && ( (year % 100) != 0 ) ) || ( (year % 400) == 0 ) ) ;
}
public String returnDate(int dateFormat){
String[] monthNames={"January","February","March","April","May","June",
"July","August","September","October","November","December"} ;
String[] abbrevMonthNames={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"} ;
StringreturnThisString =null ;
int year= 0 ;
int month= 0 ;
int day= 0 ;
year = getYear() ;
month = getMonth() ;
day= getDay() ;
switch (dateFormat){
case MMDDYY:returnThisString = month +"/" + day +"/" + (year % 100) ;
break ;
case MMMYYYY:returnThisString = abbrevMonthNames[month-1] +" " + year ;
break ;
case MMMMDDYYYY:returnThisString = monthNames[month] +" " + day +", " + year ;
break ;
default:returnThisString = month +"/" + day +"/" + (year%100) ;
}
return returnThisString ;
}
publicint getYear(){
int year = 0 ;
int numericDate = integerDate ;
for ( year = 1900; numericDate > ( isLeapYear(year) ? 366 : 365 ); year++)
numericDate -= ( isLeapYear(year) ? 366 : 365 ) ;
return year ;
}
publicint getMonth (){
int months = 0;
int numericDate = integerDate;
for(int year=1900; numericDate >=( isLeapYear(year) ? 366 : 365 ); year ++)
numericDate -= isLeapYear(year) ? 366 : 365 ;
for(months = 0; numericDate > monthTerm (getYear(),(months+1) ); months ++)
numericDate -= monthTerm(getYear(),(months + 1) );
return months;
}
publicint getDay (){
int day;
int numericDate = integerDate;
for(int year = 1900; numericDate >= ( isLeapYear(year) ? 366 : 365 ); year ++)
numericDate -= isLeapYear(year) ? 366 : 365 ;
for(int months = 0; numericDate > monthTerm(getYear(),( months + 1) ); months ++)
numericDate -= monthTerm(getYear(),(months + 1) );
for(int month = 0; numericDate > monthTerm(getYear(),( month + 1) ); month ++)
numericDate -= monthTerm(getYear(),(month + 1) );
day = numericDate;
return day;
}
privatevoid determineNumericDate(int inYear,int inMonth,int inDay){
integerDate = 0 ;
for (int year = 1900; year < inYear; year++ )
integerDate += ( isLeapYear(year) ) ? 366 : 365 ;
for (int month = 1; month < inMonth; month++ )
integerDate += ( isLeapYear(inYear) ) ? daysInLeapYear[month-1] : daysInYear[month-1] ;
integerDate += inDay ;
}
privateint queryUser(String aMessage,int minimumValue,int maximumValue){
String inputValue=null ;
intintegerValue ;
do{
inputValue=JOptionPane.showInputDialog(aMessage) ;
integerValue=Integer.parseInt(inputValue) ;
}while ( (integerValue < minimumValue) || (integerValue > maximumValue) ) ;
return integerValue ;
}
privateint monthTerm(int intYear,int intMonth){
return ( (isLeapYear(intYear) )? (daysInLeapYear[intMonth-1]):(daysInYear[intMonth-1]) );
}
privateint countDays(int intYear,int intMonth,int intDay){
int totalDays = 0;
for(int year=1900; year < intYear ; year ++)
totalDays += isLeapYear(year) ? 366 : 365;
for(int month=1; month < intMonth; month++)
totalDays += monthTerm(intYear,month);
totalDays = totalDays += intDay;
return totalDays;
}
Date(int year,int month,int day){
int dateFix;
if (year < 1900){
year = Integer.MIN_VALUE;
}elseif( month < 1 || month > 12 ){
month = Integer.MIN_VALUE;
}elseif (day < 1 || day > monthTerm(year, month) ){
day = Integer.MIN_VALUE;
}
dateFix = countDays(year, month, day);
System.out.println ("the date entered for Thanks Giving serial value is " + dateFix);
}
Date( ){
int intYear ;
int intMonth ;
int intDay ;
int intDate ;
int dateVal ;
intYear= queryUser("Enter year", 1900, Integer.MAX_VALUE) ;
intMonth= queryUser("Enter month", 1, 12) ;
intDay= queryUser("enter day", 1, (monthTerm(intYear,intMonth) ) );
intDate = countDays(intYear, intMonth, intDay);
}
}
publicclass DateObject{
publicstaticvoid main(String[] args){
DateaDate ;
Datethanksgiving ;
DateHalloween ;
aDate=new Date( ) ;
System.out.println("The date entered is " + aDate.returnDate(Date.MMMMDDYYYY) ) ;
}
}

