-5

trying to retrieve users from an ArrayList that string bookingdate equals the current date. my problem is I don't believe I am formatting my if statement correctly. the returned users will then be displayed on a datatable in a xhtml file.

  public ArrayList<User> getDinner() throws ParseException {
  Date today = new Date();
    DateFormat formatter;
    Date date;
    formatter = new SimpleDateFormat("dd/MM/yy");
    date = formatter.parse(bookingdate);

    ArrayList<User> subset = new ArrayList<User>();
    for (User user : userList) {

        if (((today.equals(date))) && (1700 < user.bookingtime)) {
            subset.add(user);
        }

    }
    return subset;
}

above is where I am having the problem below is the rest of the file.

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import java.io.Serializable;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 import javax.faces.event.ValueChangeEvent;

@ManagedBean(name = "user")
@SessionScoped
public class UserData implements Serializable {private static final long serialVersionUID = 1L;
private String name;
private String last;
private String email;
private String phone;
private String size;
Integer bookingtime;
String bookingdate;
private String requirements;
private String table;
private String userName;


public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getLast() {
    return last;
}

public void setLast(String last) {
    this.last = last;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getSize() {
    return size;
}

public void setSize(String size) {
    this.size = size;
}

public Integer getBookingtime() {
    return bookingtime;
}

public void setBookingtime(Integer bookingtime) {
    this.bookingtime = bookingtime;
}

public String getBookingdate() {
    return bookingdate;
}

public void setBookingdate(String bookingdate) {
    this.bookingdate = bookingdate;
}

public String getRequirements() {
    return requirements;
}

public void setRequirements(String requirements) {
    this.requirements = requirements;
}

public void settable(String table) {
    this.table = table;
}

public String gettable() {
    return table;
}

private static final ArrayList<User> userList
        = new ArrayList<User>(Arrays.asList(
                        new User("Janet", "Harris", "Janet12@hotmail.co.uk", "07714662361", "3", 1910,
                                "12/05/2015", "Flowers", null),
                        new User("Amy", "Harris", "Amy1234@hotmail.co.uk", "07714662361",
                                "4", 1930, "05/03/2015", "Vegan", null),
                        new User("Louise", "Hardbattle", "LouiseHardbattlex@hotmail.co.uk", "01512242901",
                                "6", 1600, "13/05/2015", "Friends birthday", null),
                        new User("Mark", "Jenkins", "MarkJ2013@live.co.uk", "07866406591",
                                "2", 1900, "13/05/2015", "N/A", null),
                        new User("Peter", "Lunn", "Plunn@hotmail.com", "01514422398",
                                "2", 1930, "13/05/2015", "First time here", null),
                        new User("Lee", "Allen", "leeallen1212@hotmail.co.uk", "07861106591",
                                "2", 1945, "13/05/2015", "N/A", null),
                        new User("Jess", "Maloney", "Jess2015maloney@live.com", "07713980273",
                                "5", 1300, "14/05/2015", "18th birthday party - please provide balloons", null),
                        new User("Christian", "Riley", "Riley1980@hotmail.com", "01514442938",
                                "4", 1400, "14/05/2015", "N/A", null),
                        new User("Carl", "Lunt", "CarlL101@live.com", "07844408012",
                                "2", 1430, "14/05/2015", "N/A", null),
                          new User("Peter", "Moore", "PeterM@hotmail.com", "01512324289",
                                "2", 1830, "14/05/2015", "Anniversary", null
                        )));

public ArrayList<User> getCustomers() {

    return userList;
}


public ArrayList<User> getDinner() {
   DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    ArrayList<User> subset = new ArrayList<User>();
    for (User user : userList) {

        if (( user.bookingdate.equals(df) ) &&(1700 < user.bookingtime)) {
            subset.add(user);
        }

    }
    return subset;
}

public ArrayList<User> getLunch() {
    ArrayList<User> subset = new ArrayList<User>();
    for (User user : userList) {

        if (1700 > user.bookingtime) {
            subset.add(user);
        }

    }
    return subset;
}

public ArrayList<User> getSearch() {
    ArrayList<User> subset = new ArrayList<User>();
    for (User user : userList) {

        if (userName.equals(user.bookingdate) || user.bookingdate == null) {
            subset.add(user);
        }

    }
    return subset;

}

public String saveAction() {

    //get all existing value but set "editable" to false 
    for (User user : userList) {
        user.setEditable(false);
    }

    //return to current page
    return null;

}

public String editAction(User user) {

    user.setEditable(true);
    return null;
}

public String deleteAction(User user) {

    userList.remove(user);
    return null;
}

public String addAction() {

    User user = new User(this.name, this.last,
            this.email, this.phone, this.size, this.bookingtime, this.bookingdate, this.requirements, null);

    userList.add(user);
    return null;
}
Baz627
  • 11
  • 3
  • 2
    You haven't actually told us what's wrong with your code. What input do you give it, what is your expected output, what is your actual output? A [MCVE](http://stackoverflow.com/help/mcve) would help, too. – azurefrog Mar 05 '15 at 18:09
  • Your problem is that `user.bookingdate` is a `String`, whereas `df` is a `DateFormat`. They are different types of objects, and will *never* be equals to each other. – azurefrog Mar 05 '15 at 19:05
  • What you want to do is use `df` to parse `user.bookingdate` and compare that against the current date. – azurefrog Mar 05 '15 at 19:06
  • See [how to compare dates](http://stackoverflow.com/questions/2592501/how-to-compare-dates-in-java) for more info on how to do the comparison once you've converted your date string into a `Date`. – azurefrog Mar 05 '15 at 19:10
  • I have updated the code, however I am now getting a java.lang.NullPointerException message :( – Baz627 Mar 05 '15 at 19:38
  • Keep in mind that using that formatter on a string such as "14/03/15" won't give you a date that will exactly equal a `Date` created on march 14th, since `Date now = new Date();` has things like hours and seconds in it, whereas your parser created `Date` will not. – azurefrog Mar 05 '15 at 19:38
  • so I need to format the current date? – Baz627 Mar 05 '15 at 19:50
  • yes, I wrote up how to do that as an answer. – azurefrog Mar 05 '15 at 20:08
  • java.lang.NullPointerException getting this error when i load the jsf page that uses the getDinner method – Baz627 Mar 08 '15 at 13:40
  • Just saying you got a NPE doesn't help much without also showing the invoking code and a full stack trace. – azurefrog Mar 08 '15 at 20:48

1 Answers1

0

The problem you are having is that when you create a new Date(), the time that it represents is the exact date and time at which it is created. However, you are comparing it to a date based on your bookingtime which only has the day/month/year.

What you need to do is to use your formatter on your new date, so that it also won't have hours, minutes, seconds, etc:

public ArrayList<User> getDinner() throws ParseException {
    DateFormat formatter = new SimpleDateFormat("dd/MM/yy");
    Date date = formatter.parse(bookingdate);
    Date today = formatter.parse(formatter.format(new Date())); // just the date

    ArrayList<User> subset = new ArrayList<User>();
    for (User user : userList) {

        if (((today.equals(date))) && (1700 < user.bookingtime)) {
            subset.add(user);
        }

    }
    return subset;
}
azurefrog
  • 10,785
  • 7
  • 42
  • 56