I use the following code to retrieve the current date and time, then add it to table using following method.
Change 2013-07-29 23:20:34.0 to 29th July 2013, 11:20 PM
DateTime
public Date getCurrentDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
System.out.println("current:" + dateFormat.format(date));
return date;
}
Hibernate
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
public Date getCurrentDateTime() {
return date;
}
To change the day to ordinal number you need to use the following suffix. have a look at this link as well
Also use the fmt library to format the date on jsp pages
A
Date
object doesn't have a format. (Neitherjava.sql.Date
norjava.util.Date
.) The database will just store it in the table in whatever native representation it chooses (almost certainly not text) and when you retrieve the data it's up to you to format it how you wish.To change the format used by
System.out.println
, just change the pattern you're passing to theSimpleDateFormat
constructor - see the documentation for details. Note that you should consider which time zone andLocale
you're interested in, too - for example, month names won't be the same between different locales, and different locales also have different expected formats. (Even within the same language, there are different conventions.)You'll find the "th" of "29th" hard to handle with
SimpleDateFormat
- I don't believe it supports ordinals. This question may help you though. (It's going to be ugly code, mind you - I would suggest changing the format to remove the ordinal if you possibly can.)Something similar to this ??
for hibernate