Jump to content
Sign in to follow this  

Converting string date to sql Date format in java

Recommended Posts

I have date in string format as e.g. "2015-05-12 15:15:24", I would like to convert it to sql date in the format "dd-MMM-yy". However, this is not working. Below is the code snippet:

    String rawDate="2015-05-12 15:15:24";

    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date =format.parse(rawDate);
    Date sqlDate = new java.sql.Date(date.getTime());

    SimpleDateFormat changedFormat = new SimpleDateFormat("dd-MMM-yy");
    Date date2=changedFormat.parse(changedFormat.format(sqlDate));
    Date sqlDate2 = new java.sql.Date(date2.getTime());

    System.out.println("sqlDate : "+sqlDate +"  ::::::   Date2 : "+date2+"  :::: sqlDate2  "+sqlDate2);ow here is the Test : "+sqlDate2);

The output of the program is :

sqlDate : 2015-05-12 :::::: Date2 : Tue May 12 00:00:00 BST 2015 :::: sqlDate2 2015-05-12

The aim was to get date in the format of 12-May-15 java.sql format, but May is not being translated into alphabet month rather its printed as number.

Share this post

Link to post
Share on other sites
This topic is now closed to further replies.
Sign in to follow this