File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Formatting the String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Formatting the String" Watch "Formatting the String" New topic
Author

Formatting the String

Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
Hi,

I am getting the date field from oracle database as String like "2004-02-04 00:00:00.0" .I need to convert this string to mm/dd/yy format.How to do this.I tried using simpledateformat but didnt work.

Regards
Vasu
Wang Kaiyi
Greenhorn

Joined: Feb 02, 2004
Posts: 9
I believe that this will help you...
I think you may study "dateformat" carefully...
package com.kaiyi.test;
import java.text.*;

public class TestSqlDate {
public TestSqlDate() {
}
public static void main(String[] args) {
TestSqlDate testSqlDate1 = new TestSqlDate();
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
java.text.SimpleDateFormat simple = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sDate = "2004-02-25 12:12:25";
java.util.Date dDate = new java.util.Date();
try {
dDate = simple.parse(sDate);
}
catch (ParseException ex) {
ex.printStackTrace();
}
System.out.println("------date-------"+dDate.toString());
simple.applyPattern("yyyy-MM-dd HH/mm/ss");
System.out.println("------new Date-------"+simple.format(dDate));
}
}
Vasudevan Gopalan
Ranch Hand

Joined: Aug 29, 2003
Posts: 67
Thanks Wang...
Harro deGouw
Greenhorn

Joined: Feb 23, 2004
Posts: 5
Even better: let your oracle database do the date conversion. The string you get from Oracle when calling getString on your resultset is locale dependant and bound to create unstable code.
Consider this:
select sysdate from dual --> 25-2-04 15:39:11 (25-2 is specific for my locale settings)
better would be:
select to_char(sysdate, 'mm dd yyyy') as mydate from dual
--> will ALLWAYS return date in the month date year format
Third option: use the getDate method on the resultset. This returns a java.sql.Date object with which you can create a java.util.Date:
Date myDate = new java.util.Date(result.getDate("colname").getTime())
Bikramjit Singh Bajwa
Ranch Hand

Joined: Nov 23, 2003
Posts: 39
I think, you are retrieving date from the database as a String instead of as a date. First of all retrieve date as a date and then parse it in to java.util.Date class becaosue database return date object of java.sql.Date class. So you will have to parse it first into util package's date object. then you can use SimpleDateFormat Class. It takes paraeter as date object in the constructor.
Chakk de
Bikram
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Formatting the String