Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Format

 
selvi family
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I had a doubt in NumberFormat to display the US currency.It was cleared through javaranch.

Now I have a doubt in DateForamt...

I am using Oracle data base to store the date.
In data base the stored date is fetched as "2005-01-06 04:28:42.0"

Is that possible to display the sais date in "Jan 6, 2005" format.

I tried the following code:
sodate is a arraylist:

The datestring variable stores the value "2005-01-06 04:28:42.0"

DateFormat df=DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.US);
Date date2 = df.parse(datestring);
out.println(df.format(date2));

I got the ParseException...

How to solve this..
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you get the value from the DB as a Date object, rather than as a String you then parse. Then you apply the formating you need by defining it in your SimpleDateFormat (read the docs) and calling the format method.
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get you started you are going to need two SimpleDateFormats:
One to parse the date being passed in

DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");

and one to format the date to the way you want it to look

DateFormat df=new SimpleDateFormat("MMM d, yyyy");

Post back if you are having problems!
 
Jonas Isberg
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this (highly inspired by Barry's post) should work.
 
selvi family
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of U....My prob is solved...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic