【发布时间】:2018-08-31 12:57:20
【问题描述】:
我有一个露营程序,它有一个对话框,用户可以在其中输入他们要签入的日期以及他们计划签出的日期到一个文本字段中。一旦他们在对话框上按下确定,他们输入的日期就应该可以在 JTable 中查看。目前,在日期列中,当用户按下确定时,我得到一个 java.utl.GregorianCalendar[time....etc]。姓名、停留天数和站点编号等其他信息显示正常。显然它没有正确格式化日期,但我不确定如何修复它。这是 DialogCheckInTent 类中公历的代码:
public GregorianCalendar getDateIn(){
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
GregorianCalendar g = new GregorianCalendar();
try{Date date = df.parse(dateIn.getText());
g.setTime(date);
}
catch (ParseException ex) {
ex.printStackTrace();
}
return g;
}
public GregorianCalendar getCheckOutDate(){
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
GregorianCalendar g = new GregorianCalendar();
try{Date date = df.parse(checkOutTxt.getText());
g.setTime(date);
}
catch (ParseException ex) {
ex.printStackTrace();
}
return g;
}
当用户按下确定时,上面的代码在 GUI 中的这个方法中使用。
if (pressed == checkInTentItem) {
newTent.clear();
newTent.setVisible(true);
if (model.getRowCount() < 5) {
if (newTent.isOk()) {
String nameReserving = newTent.getNameReserve();
GregorianCalendar checkIn = newTent.getDateIn();
int daysStaying = newTent.getDaysStaying();
GregorianCalendar checkOutOn = newTent.getCheckOutDate();
int siteNumber = newTent.getSiteNumber();
int tenters = newTent.getTenters();
Tent tent = new Tent(nameReserving, checkIn, daysStaying,
checkOutOn, siteNumber, tenters);
model.add(tent);
}
}
else {
JOptionPane.showMessageDialog(frame, "All sites are full.",
" ", JOptionPane.INFORMATION_MESSAGE);
}
}
一切编译都没有问题,但日期工作不正常。我该如何解决这个问题?
【问题讨论】:
标签: java date datetime gregorian-calendar