aspose file tools*
The moose likes JDBC and the fly likes Modifying the TimeStamp data type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Modifying the TimeStamp data type" Watch "Modifying the TimeStamp data type" New topic
Author

Modifying the TimeStamp data type

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
I was wondering if anyone knew how to modify the timestamp data type so that it output like: DD MMM YYYY HH:NN:SS
Has anyone done this? I tried to modify Timestamp.java in the java.sql directory but its a bit messy. Basically when I output a table using select * from tablename I get the date in the usual timestamp format and it doesn't look as nice as the format i'd like.
Any ideas are welcome.
Suhail
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Timestamps are not used for formatting. Use the SimpleDateFormat class to make the timestamp appear as you want:
http://java.sun.com/j2se/1.3/docs/api/java/text/SimpleDateFormat.html
Jamie
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Yes, I agree but I am outputting from a database table using a preparedstatement such as select * from tablename. If the date is a string in the column then there are no problems and simpledateformat works but when the date column is of the type timestamp then simpledate format has no effect. The code I have is below:
PreparedStatement ps22 = connection.prepareStatement("SELECT Date, Balance FROM Purse");
rs1 = ps22.executeQuery();
while (rs1.next()){
try {
TimeZone gmt = TimeZone.getTimeZone("GMT+1");
GregorianCalendar in = new GregorianCalendar(gmt);
SimpleDateFormat sdf = new SimpleDateFormat( "dd MMM yyyy hh:mm:ss" );
sdf.setCalendar(in);
String theDate = sdf.format(in.getTime());
try {
Date date = sdf.parse(theDate);
in.setTime(date);
java.sql.Timestamp sDate = new java.sql.Timestamp(date.getTime());

int dep = 0;
String pname = "Lunar Telecoms Ltd";
String bal = rs1.getString("Balance");
PreparedStatement psi1 = connection.prepareStatement("INSERT INTO Account " +
"VALUES (?, ?, ?, ?, ?)");
psi1.setTimestamp(1,sDate);
psi1.setString(2,pname);
psi1.setInt(3,dep);
psi1.setString(4,s);
psi1.setString(5,bal);
psi1.executeUpdate();
psi1.close();
Any ideas why this outputs the date column as a timestamp and not like I wanted it (dd MMM yyyy hh:mm:ss) ???
Any help is much appreciated.
Suhail

Originally posted by Jamie Robertson:
Timestamps are not used for formatting. Use the SimpleDateFormat class to make the timestamp appear as you want:
http://java.sun.com/j2se/1.3/docs/api/java/text/SimpleDateFormat.html
Jamie

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Modifying the TimeStamp data type
 
Similar Threads
Hibernate - how to modify value before Hibernate stores it?
Oracle Date and Time problem
bad Timestamp Exception
Current date and time
Oracle Pivot table