Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Compare two date objects

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Compare two date objects

    I made a bill class that uses a date and money class to create bills.
    I have to be able to check whether the paid date is before (or on) the due date. I am unsure of how to compare the dates.

    I am doing this in the public void setPaid method.

    Bill Class:
    public class Bill{
     
        //Variables/Data
        Money amount;
        Date dueDate;
        Date paidDate;
        String originator;
     
        //Methods
     
        public Bill(Money amount, Date dueDate, String originator){
           setAmount(amount);                
           setDueDate(dueDate);
           setOriginator(originator);
     
        }
     
        public Bill(Bill toCopy){
           this.setAmount(toCopy.amount);
           this.setDueDate(toCopy.dueDate);
           this.setOriginator(toCopy.originator);
     
     
           //setAmount(toCopy.getAmount());                
           //setDueDate(toCopy.getDueDate());
           //setOriginator(toCopy.getOriginator());
        }
     
        public Money getAmount(){
            return amount;
        }
     
        public Date getDueDate(){
            return dueDate;
        }
     
        public String getOriginator(){
            return originator; 
        }
     
        public boolean isPaid(){
            if(!(paidDate == null)){
                return true;
            }else{
                return false;
            }
        }
     
        public void setPaid(Date onDay){
           if(dueDate > paidDate){
               System.out.println("The due date has passed");
          }else{
               paidDate = onDay;
           }
        }
     
        public void setUnpaid(){
            paidDate = null;
        }
     
        public void setDueDate(Date nextDate){
            dueDate = nextDate;
        }
     
        public void setAmount(Money amountGiven){
            amount = amountGiven;
        }
     
        public void setOriginator(String originatorGiven){
            originator = originatorGiven;
        }
     
        public String toString(){
            if(paidDate== null){
                if(originator == null){
                    return "An unknown company owes " + amount + " by the due date " + dueDate;
                }else{    
                    return originator + " owes " + amount + " by the due date " + dueDate;
                }
            }else{
                if(originator == null){
                    return "An unknown company owes " + " paid " + amount + " by the due date " + dueDate
                       + " it was paid on " + paidDate;
                }else{    
                    return originator + " paid " + amount + " by the due date " + dueDate
                       + " it was paid on " + paidDate;
                }
     
            }
        }
     
     
        public boolean equals(Bill toCompare){
            //null check
            if (toCompare == null){
                return false;
            }else{
                if(!getClass().equals(toCompare.getClass())){
                    return false;
                }else{    
                    Bill toCopy = (Bill) toCompare;
                    return getOriginator() == toCompare.getOriginator() &&
                           getDueDate() == toCompare.getDueDate() &&
                           getAmount() == toCompare.getAmount() ;
                }
     
            }
        }
     
    }




    Date Class:

    public class Date{
        private int month, day, year;
     
        public Date(){
            setMonth(1);
            setDay(1);
            setYear(1970);   
        }
     
        public Date(int month, int day, int year){
            setMonth(month);
            setDay(day);
            setYear(year);    
        }
     
        public Date(Date otherDate){
            setMonth(otherDate.getMonth());
            setDay(otherDate.getDay());
            setYear(otherDate.getYear());  
        }
     
        public int getMonth(){
            return month;
        }
     
        public int getDay(){
            return day;
        }
     
        public int getYear(){
            return year;
        }
     
        public void setMonth(int newMonth){
            if(newMonth < 1 || newMonth > 12){
                throw new IllegalStateException(String.format("Illegal month: %d", newMonth));  
            } else{
                month = newMonth;
            }
        }
     
        public void setDay(int newDay){
            if(newDay < 1 || newDay > 31){
                throw new IllegalStateException(String.format("Illegal day: %d", newDay));  
            } else{
                day = newDay;
            }
        }
     
        public void setYear(int newYear){
            if(newYear < 1970){
                throw new IllegalStateException(String.format("Illegal year: %d", newYear));  
            } else{
                year = newYear;
            }
        }
     
        public void setDate(int month, int day, int year){
            setMonth(month);
            setDay(day);
            setYear(year);
     
        }
     
        public String toString(){
            return String.format("%d/%d/%d", getMonth(), getDay(), getYear());
     
        }
     
        public boolean equals(Object otherObject){
            if (otherObject == null){
                return false;
            }else{
     
                if(!getClass().equals(otherObject.getClass())){
                    return false;
                }else{
                    Date otherDate = (Date) otherObject; 
                    return getMonth()== otherDate.getMonth() && 
                           getDay()== otherDate.getDay() && 
                           getYear()== otherDate.getYear();
     
                        }
     
            }    
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Compare two date objects

    unsure of how to compare the dates.
    How would you do it manually? Given two dates with day, month and year how would you compare them?

    Hint: there could need to be three comparisons made. One for each field.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Be aware that in Java if you have two objects and do an == compare, you are checking if the two objects are the same exact identical object, not if the two objects are equal. To do the later you would implement and use the equals method of the objects in question.

    Rolf

Similar Threads

  1. how do you compare words?
    By beginner123 in forum Java Theory & Questions
    Replies: 12
    Last Post: November 18th, 2012, 08:44 AM
  2. Java Date Format in Date Object
    By Ashr Raza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 13th, 2012, 10:47 AM
  3. Parsing a full date/time/timezone date to "yyyy-MM-dd"
    By Occidentally in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 4th, 2012, 08:57 AM
  4. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  5. same date should entered in another date field
    By shashib09 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 14th, 2011, 08:42 AM