Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Queston about formatting date

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
TIMESTAMP(14) ------> YYYYMMDDHHMMSS
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

System.out.println(sdFormat.format(date));
}
}

Marilyn
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic