尝试从ArrayList检索字符串,该字符串是一个等于当前日期的字符串。我的问题是我不相信我正确格式化我的if语句。然后,返回的用户将显示在xhtml文件中的数据表中。
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;
}
以上是我遇到问题的地方是文件的其余部分。
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;
}
答案 0 :(得分:0)
您遇到的问题是,当您创建new Date()
时,它所代表的时间是创建它的确切日期和时间。但是,您将其与基于bookingtime
的日期进行比较,该日期仅包含日/月/年。
您需要做的是在新的日期使用格式化程序,这样它也不会有小时,分钟,秒等等:
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;
}