my dog learned polymorphism
The moose likes Beginning Java and the fly likes Queston about formatting date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Queston about formatting date" Watch "Queston about formatting date" New topic

Queston about formatting date

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
I use MySQL for my project, and my customer_order table has a column called "date_of_order", and I want this date format to be like this:
Monday, April 14, 2003 4:01:20 AM
It seems that MySQL can't do this. According to the MySQL doc, the best we can have is:
But, that is fine if later on my Java application can retrieve the date and contruct it into the format I want.
I don't know how to do this. Do you have any idea? Thanks.
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hi Gene,
Here's the code giving the from and to formats.
from format is what your database stores it as and to format is what you want that to be converted to. I found it to be working fine.
import java.text.*;
import java.util.*;
class DateEx{
public static void main (String args[]) throws ParseException{
String fromFormat = "yyyyMMddhhmmss";
String dateString = "20030922122150";
String toFormat = "EEEE, MMMM dd, yyyy hh:mm:ss a";
SimpleDateFormat sdFormat = new SimpleDateFormat(fromFormat);
Date date = sdFormat.parse(dateString);
sdFormat = new SimpleDateFormat(toFormat);


Joel McNary

Joined: Aug 20, 2001
Posts: 1824

In java, how dates are stored in the Database is (mostly) invisible to the developer. When you get a result set and call getDate (or getTime or getTimestamp), it will return a java.sql.Date (or java.sql.Time or java.sql.Timestamp, depending on the data type) representation of the date in the database. These classes are subclasses of java.util.Date and can therefore be passed to SimpleDateFormat to display them in any format that you wish.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Queston about formatting date
It's not a secret anymore!