8 Replies - 1078 Views - Last Post: 09 June 2012 - 10:42 PM Rate Topic: -----

#1 JavaProbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 12

Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 05:12 PM

I'm trying to get the program to ask the user for a date but I keep getting my system's date and time in the output.. It's actually a month faster then my computer. Not sure if that is a problem.
public class Date {
	private int month; // instance variable for value of the date’s month
	private int day; // instance variable for value of the date’s day
	private int year; // instance variable for the value of the dates
	private int time; // instance variable for the value of the date's time
	
	// Default constructor: set the instance variables to default values: month = 1; day = 1; year = 1900;
	public Date() {
		month = 1;
		day = 1;
		year = 1900;
	}

	// Constructor to set the date
	// The instance variables month, day, and year are set according to received parameters.
	public Date(int month, int day, int year) {
		this.month = month;
		this.day = day;
		this.year = year;
	}

	// Method to set the date
	// The instance variables month, day, and year are set according to received parameters.
	public void setDate(int month, int day, int year)

	{
		this.month = month;
		this.day = day;
		this.year = year;
	}

	// Method to return the date’s month value
	public int getMonth() {
		return month;
	}

	// Method to return the date’s day value
	public int getDay() {
		return day;
	}	
	// Method to return the date's time value
	public double getTime()
	{
		return time;
	}

	// Method to return the date’s year value
	public int getYear() {
		return year;
	}

	// Override of inherited toString() method to return the date in the form of mm-dd-yyyy
	public String toString() {
		return (month + "-" + day + "-" + year);
	}
}






import java.text.DateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class ExtendedDate extends Date
{
	

	Calendar calendar = new GregorianCalendar();
    private static final int[] daysInMonth = { 0, 31, 29, 31, 30, 31, 30, 31,
        31, 30, 31, 30, 31 };
	int year       = calendar.get(Calendar.YEAR);
	int month      = calendar.get(Calendar.MONTH); 
	int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH); // Jan = 0, not 1
	int dayOfWeek  = calendar.get(Calendar.DAY_OF_WEEK);
	int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
	int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);

	int hour       = calendar.get(Calendar.HOUR);        // 12 hour clock
	int hourOfDay  = calendar.get(Calendar.HOUR_OF_DAY); // 24 hour clock
	int minute     = calendar.get(Calendar.MINUTE);
	int second     = calendar.get(Calendar.SECOND);
	int millisecond= calendar.get(Calendar.MILLISECOND);
	
	public static void main(String args[])
	{
	Calendar calendar = new GregorianCalendar();
	Calendar calendar2 = new GregorianCalendar();
	
	calendar2.set(Calendar.DAY_OF_MONTH, 8);
    
	System.out.println("Cal1 YEAR: " + calendar.get(Calendar.YEAR));
	System.out.println("Cal1 MONTH:" + calendar.get(Calendar.MONTH));
	System.out.println("Cal1 DAY:  " + calendar.get(Calendar.DAY_OF_MONTH));
	
	System.out.println("Cal2 YEAR: " + calendar.get(Calendar.YEAR));
	System.out.println("Cal2 MONTH:" + calendar.get(Calendar.MONTH));
	System.out.println("Cal2 DAY:  " + calendar.get(Calendar.DAY_OF_MONTH));
	}
	public ExtendedDate() {
        super();
    }
 
//Constructor that accepts parameters (2)
    public ExtendedDate(int month, int day, int year) {
     
        super(month, day, year);
    }
 
//Method to set the date – override inherited setDate method
//If data is invalid, do not change the receiver (2)
    public void setDate(int monthInt, int dayInt, int yearInt)
        {
        setDate(getMonth(), getDay(), getYear());
         
        }
 
//Method to set month ensuring that only valid changes are made 
    public void setMonth(int anInt)
        {
        getMonth();
        }
 
//Method to set day ensuring that only valid changes are made 
    public void setDay(int anInt)
        {
        getDay();
        }
 
//Method to set year ensuring that only valid changes are made 
        {
        getYear();
        }
     
 
//New method to verify a date
//Valid range is 1-1-1900 through 12-31-2999
    public static boolean isGodDate(int month, int day, int year)
        {
        if (1900 < year || year < 2999)
            return false;
        if (month < 1 || month > 12 )
            return false;
        if (day < 1 || day > daysInMonth[month])
            return false;
        if (month == 2 && day == 29 && !isLeapYear(year))
            return false;
            return true;
        }
 
//check for a leap year
    public static boolean isLeapYear(int aYear)
        {
        if (aYear % 400 == 0)
        {
            return true;
        }
        else if (aYear % 100 == 0)
        {
        	return false;
        }
        else if (aYear % 4 == 0)
        {
        	return  true;
        }
        else
        	return false;
        }
 
//Instance method version of the above (7g)
    public boolean isLeapYear()
        {
            return ExtendedDate.isLeapYear(getYear());
        }
 
//Return number of days in a month 
    public int daysInMonth()
        {
        if (getMonth() >= 1 && getMonth() <= 12)
            if (getMonth() != 2)
                return daysInMonth[getMonth()];
            else if (isLeapYear(getYear()))
                return 29;
            else
                return 28;
        else
            return 0;
        }
 
 
    public int daysToDate()
        {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.set(weekOfYear, month, dayOfMonth);
        end.set(weekOfYear, month, dayOfMonth);
        java.util.Date startDate = start.getTime();
        java.util.Date endDate = end.getTime();
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        long diffTime = endTime - startTime;
        long diffDays = diffTime / (1000 * 60 * 60 * 24);
        DateFormat dateFormat = DateFormat.getDateInstance();
        System.out.println("The difference between "+
          dateFormat.format(startDate)+" and "+
          dateFormat.format(endDate)+" is "+
          diffDays+" days.");
		return dayOfMonth;

        }  
        
 
//Return number of days remaining in year (7j)
    public int daysRemainingInYear()
        {
    	Calendar c1 = Calendar.getInstance();
    	int doy1 = c1.get(Calendar.DAY_OF_YEAR);
    	
    	int year = c1.get(Calendar.YEAR);
    	
    	Calendar calendar2 = new GregorianCalendar(month,getDay(), year);
    	int doy2 = calendar2.get(Calendar.DAY_OF_YEAR);
    	
    	int days = doy2 = doy1;
    	System.out.println(days + " days remain in current year");
		return days;
        }
        
 
//Create a new GoodDate object with the values that result from adding a number of days to an existing GoodDate object.  Cannot result in an invalid GoodDate being created.  If an invalid date would be created, return a GoodDate with default values. (7k)
    public ExtendedDate futureDate(int Day) 
    {
    	  Calendar start = Calendar.getInstance();
          Calendar end = Calendar.getInstance();
          start.set(weekOfYear, month, dayOfMonth);
          end.set(weekOfYear, month, dayOfMonth);
          java.util.Date startDate = start.getTime();
          java.util.Date endDate = end.getTime();
          long startTime = startDate.getTime();
          long endTime = endDate.getTime();
          long diffTime = endTime - startTime;
          long diffDays = diffTime / (1000 * 60 * 60 * 24);
          DateFormat dateFormat = DateFormat.getDateInstance();
          System.out.println("The difference between "+
            dateFormat.format(startDate)+" and "+
            dateFormat.format(endDate)+" is "+
            diffDays+" days.");
  		return new ExtendedDate();
    
    
	}
    	
  }    

*** EDIT ***
Please use a more descriptive title than "Problem"
Posted Image

This post has been edited by GunnerInc: 09 June 2012 - 05:19 PM
Reason for edit:: Added code tags and a more descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Problem - Asking the user for a date but get system date instead

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 09:23 PM

View PostJavaProbie, on 09 June 2012 - 08:12 PM, said:

I'm trying to get the program to ask the user for a date


Where ?
Was This Post Helpful? 0
  • +
  • -

#3 JavaProbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 12

Re: Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 09:42 PM

I guess I need to know where in the code it is reading it from my machine and change to scan for user input.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 09:45 PM

Yes that would help :) But as it is your code you;ll find it faster than I can.
Was This Post Helpful? 0
  • +
  • -

#5 JavaProbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 12

Re: Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 09:46 PM

I also need to override the setDate method.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 09:48 PM

if you say so
But, just as a hint, you'll need to extend the actual class if you want to overload one of its method
Was This Post Helpful? 0
  • +
  • -

#7 JavaProbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 12

Re: Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 10:14 PM

I believe this extends my date class

063 import java.text.DateFormat;
064 import java.util.Calendar;
065 import java.util.GregorianCalendar;
066
067 public class ExtendedDate extends Date
068 {

this is an argument and I was given the methods but had to set the arguments. I'm trying to change the argument to scan for input.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 10:19 PM

View PostJavaProbie, on 10 June 2012 - 01:14 AM, said:

I believe this extends my date class


You believe ? You wrote this code yes or no ? Is this your date class ?
Was This Post Helpful? 0
  • +
  • -

#9 JavaProbie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-February 12

Re: Problem - Asking the user for a date but get system date instead

Posted 09 June 2012 - 10:42 PM

Yes, I BELIEVE this extends my date class. You just realized that you didn't see that part of the code and are now resorting to insults to compensate for your mistake and for your small penis. If you log on here to make yourself feel superior, please go back to your late night porn and give it a tug until your finished and then it the rack. You are beyond useless. My dogs have a better chance of helping me then you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1