Changing one date format to another

10,899

Solution 1

You shouldn't have to format dates to insert them in a database. If using JDBC, use prepared statements.

To answer your question, though, m can't mean minute and month at the same time. M means month. m means minute.

Solution 2

This code outputs needed for you result:

Date dte = new Date();//or something else
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
String formattedDate = formatter.format(dte);
System.out.println(formattedDate);
Share:
10,899
user964819
Author by

user964819

Updated on June 14, 2022

Comments

  • user964819
    user964819 almost 2 years

    I 'm getting the date in the following format (Date and not String):

    Tue Jun 26 07:00:00 EDT 2012
    

    I want to change the format of the date to (Date):

    6/26/2012 10:19:15 AM 
    

    so as to update the same in the data base. I tried following code:

    Date dte;
    Date dte1;(Tue Jun 26 07:00:00 EDT 2012)
    SimpleDateFormat formatter = new SimpleDateFormat("m/dd/yyyy hh:mm:ss a");
    String formattedDate = formatter.format(dte1);
    dte  = formatter.parse(formattedDate);
    SystemUtils.trace("test", " date>>>" + dte); 
    

    is yielding the following response:

    Thu Jan 26 07:00:00 EST 2012
    

    Can any one please share the piece of code to do the same asap.