• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

How to convert the date format 19-Mar-15 to 19-Mar-2015 in java

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to convert the date format 19-Mar-15 to 19-Mar-2015 in java
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to use SimpleDateFormat which can be imported from java.text.* API.

Output is as followes:

Date 1 : 20-Mar-15
Date 2: 20-Mar-2015



Link for SimpleDateFormat java docs:java.lang.SimpleDateFormat
 
Nagaraju Yanamala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Minal Abhijit Urankar,

Actually i am getting string form of date object in jsp.

like,

<%=checkForNull((String)resultset.get("MAKER_DATE"))%>

Here Maker date value displaying like '19-Mar_15' , But i want display format like '19-Mar-2015'.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently the date you get is a String, not a Date object.

You can parse the string using a SimpleDateFormat with the pattern "dd-MMM-yy", and then format the resulting Date object with another SimpleDateFormat with the pattern dd-MMM-yyyy.
 
Master Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you storing a date value in a database as a String?
If it was a DATE then you could either format using a SimpleDateFormat (as mentioned) or change the SQL to return the DATE as a pre-formatted String.

As it stands you now have to convert the String to a Date and the back again to a String...
 
Nagaraju Yanamala
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Minal Abhijit Urankar,

it's working fine.

I Have written the code like this,


<%
String s_Date=checkForNull((String)resultset.get("MAKER_DATE"));
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy");
Date date = formatter.parse(s_Date);
SimpleDateFormat ft1 = new SimpleDateFormat ("dd-MMM-yyyy");
String s_Cdate=ft1.format(date);
%>



Thanks for your helping.....
 
Marshal
Posts: 72061
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If at all possible stop using Date and use this instead (or this).
 
Dave Tolls
Master Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just noticed as well.
Why are you using get() from ResultSet and then casting to String rather than using the getString() method on ResultSet.
You know the datatype after all.
 
Look! I laid an egg! Why does it smell like that? Tiny ad, does this smell weird to you?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic