我正在尝试格式化日期以获取特定格式的日期。但它不起作用。不知道为什么!这是我做的:
for(LeaveApply leaveApply : leaveApplyList) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
leaveApply.setDate(dateFormat.parse(dateFormat.format(leaveApply.getDate())));
} catch (ParseException e) {
e.printStackTrace();
}
}
输出结果为:
LeaveApply [id=1, user=id [1]; loginName [admin], leaveType=Medical Leave, leaveTime=Half Leave, date=Fri Apr 15 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 02 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sun Apr 03 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 09 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sun Apr 10 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 16 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sun Apr 17 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 23 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sun Apr 24 00:00:00 IST 2011]
LeaveApply [id=0, user=id [1]; loginName [admin], leaveType=Weekend, leaveTime=Full Leave, date=Sat Apr 30 00:00:00 IST 2011]
如您所见,日期不是所需格式(dd-MM-yyyy)。任何帮助都会很明显。 LeaveApply类是:
public class LeaveApply implements Serializable {
private static final long serialVersionUID = -7223672229309295181L;
private long id;
private User user;
private String leaveType;
private String leaveTime;
private Date date;
public LeaveApply() {
}
public LeaveApply(User user, String leaveType, String leaveTime, Date date) {
super();
this.user = user;
this.leaveType = leaveType;
this.leaveTime = leaveTime;
this.date = date;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getLeaveType() {
return leaveType;
}
public void setLeaveType(String leaveType) {
this.leaveType = leaveType;
}
public String getLeaveTime() {
return leaveTime;
}
public void setLeaveTime(String leaveTime) {
this.leaveTime = leaveTime;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "LeaveApply [id=" + id + ", user=" + user + ", leaveType=" + leaveType + ", leaveTime=" + leaveTime + ", date=" + date + "]";
}
}
我将列表打印为:
for (LeaveApply leaveApply : leaveApplyList) {
System.out.println(leaveApply);
}
答案 0 :(得分:2)
打印时,您需要使用SimpleDateFormat格式化字符串。你的代码现在做的是:
这是一个相当昂贵的无操作,因为Date对象只包含一个表示时间戳的长整数,没有格式化信息。
此外,在您打印时间戳的位置,您使用默认日期格式。您应该使用此时格式化的日期。
答案 1 :(得分:2)
在SimpleDateFormat.format(Date)
实施中调用LeaveApply.toString()
以获得所需的输出。
答案 2 :(得分:1)
在格式化时,您将在String中获得所需的格式,当您解析它时,您将获得Date对象,它将执行toString()
,因此您将始终获得默认格式。