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) ) ;

}

}

[11307 byte] By [eveningsa] at [2007-11-27 1:49:14]
# 1
I'll ask the same question I asked in your other post: What is the "integer representation of a date"?And an additional question, Why are you reinventing the wheel? Why are you not using Java's Date and/or Calender classes? (ok, that's 2 questions.)
ChuckBinga at 2007-7-12 1:14:17 > top of Java-index,Java Essentials,New To Java...
# 2

> I'll ask the same question I asked in your other

> post: What is the "integer representation of a

> date"?

>

I am new to java and have taken to this project to learn. What I mean by integer representation is this.

The program will ask for 3 numbers.. a year a month and a day. I want to output that information like this..say I put 1901 for year and 01for month and 1 for day the output would show January 1, 1901. Also the integer representation for that date would be 366. That is 366 days from Jan 1 1900.

eveningsa at 2007-7-12 1:14:17 > top of Java-index,Java Essentials,New To Java...
# 3

If you want to play with this code for practice, that's ok. But if you want to learn good Java programming your code is not ok.

Here is a basic program (it's not object-oriented, it doesn't validate input values or that the end date is greater than the beginning, among other things). While this isn't represented to be a good Java program, it is an example of the use of the Java libraries.

(Oh - and by the way, 1900 is not a leap year and correct number of days is 365, not 366 as you said.)

import java.util.Calendar;

import java.util.Date;

import java.util.GregorianCalendar;

public class Xyy

{

public static void main(String[] args)

{

Calendar baseDate = new GregorianCalendar(1900, Calendar.JANUARY, 01);

System.out.println(baseDate.getTime());

int year = 1901;

int month = 1;

int day = 1;

Calendar inputDate = new GregorianCalendar(year, month-1, day);

System.out.println(inputDate.getTime());

int dayCount = 0;

do

{

baseDate.add(Calendar.DATE, 1);

dayCount++;

}while (!baseDate.equals(inputDate));

System.out.println(dayCount + " days");

}

}

ChuckBinga at 2007-7-12 1:14:17 > top of Java-index,Java Essentials,New To Java...
# 4
> (Oh - and by the way, 1900 is not a leap year> and correct number of days is 365, not 366 as you> said.)I never said 1900 was a leap year.. i said that 366 is Jan 1 1901. 365 is Dec 31 1900.
eveningsa at 2007-7-12 1:14:17 > top of Java-index,Java Essentials,New To Java...
# 5
You count funny. Jan 2, 1900 is 1 day from Jan 1, 1900. Dec 31, 1900 is 364 days from Jan 1, 1900Jan 1, 1901 is 365 days from Jan 1, 1900.
ChuckBinga at 2007-7-12 1:14:17 > top of Java-index,Java Essentials,New To Java...