Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Formatting the String

 
Vasudevan Gopalan
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Wang...
 
Harro deGouw
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic