I am trying to format Date to get a Date of a specific format. However it is not working. Don’t know why! Here is what I did:
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();
}
}
The output is :
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]
As you can see the date is not in desired format (dd-MM-yyyy). Any help will be appreciable.
The LeaveApply class is :
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 + "]";
}
}
And I am printing the list as :
for (LeaveApply leaveApply : leaveApplyList) {
System.out.println(leaveApply);
}
You need to use the SimpleDateFormat to format the string when you print it. What your code does now is:
Which is a rather expensive no-op, as a Date object only contains a long representing the timestamp, no formatting information.
Further on, where you print the timestamp, you use the default date format. You should use your date formatted at that point instead.