我找不到任何有趣的信息如何在JavaFX2上存储时间和日期,并能够将它绑定到Label。我想我可以扩展StringProperty用于我自己的目的,但我不确定如何实现所有这些方法。此外,我有一个列表包含很多位置,我希望能够过滤此列表,例如仅显示3年以上的型号。有没有好办法呢?
修改 我目前的解决方案
public class DateProperty extends SimpleStringProperty {
private final Calendar calendar = Calendar.getInstance();
public DateProperty() {
}
public Date getDate() {
return calendar.getTime();
}
public Calendar getCalendar() {
return calendar;
}
public void set(String a) {
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
try {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
calendar.setTime(formatter.parse(a));
} catch (ParseException e) {
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
try {
calendar.setTimeInMillis(formatter.parse(a).getTime()
+ calendar.getTimeInMillis());
} catch (ParseException e1) {
calendar.setTimeInMillis(0);
}
}
}
abstract class DateBinding extends StringBinding {
abstract protected DateFormat getFormatter();
@Override
protected String computeValue() {
return getFormatter().format(calendar.getTime());
}
}
public StringBinding asDateString() {
return new DateBinding() {
@Override
protected DateFormat getFormatter() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
}
public StringBinding asTimeString() {
return new DateBinding() {
@Override
protected DateFormat getFormatter() {
return new SimpleDateFormat("HH:mm:ss");
}
};
}
public StringBinding asDateAndTimeString() {
return new DateBinding() {
@Override
protected DateFormat getFormatter() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
};
}
}