-1
/**
 * 
 * Purpose:
 * 
 * 1. Need to convert a String date of format "dd/MM/yyyy HH:mm:ss" to
 * java.util.Date of format dd/mm/yyyy HH:mm:ss and convert that to
 * java.sql.Date to send to database table column of type Date.
 *
 */
public class TestDate {

    public static void main(String[] args) throws ParseException {
        String inputDateStr = "10-Jan-2013";
        SimpleDateFormat inputFormatter = new SimpleDateFormat("dd-MMM-yyyy");
        SimpleDateFormat outputFormatter = new SimpleDateFormat(
                "dd/MM/yyyy HH:mm:ss");
        java.util.Date inputDateDate = inputFormatter.parse(inputDateStr);
        System.out.println("inputDateDate :" + inputDateDate);
        // printed: inputDateDate :Thu Jan 10 00:00:00 EST 2013

        String requiredDateStr = outputFormatter.format(inputDateDate);
        System.out.println("requiredDateStr :" + requiredDateStr);
        // printed: requiredDateStr :10/01/2013 00:00:00

        /*
         * PROBLEM: Need to convert this 'requiredDateStr' ie: '10/01/2013
         * 00:00:00' to a java.util.Date type and still retain the pattern of
         * "dd/MM/yyyy HH:mm:ss"
         */

        //how to do this?

        // java.util.Date requiredDateDate = feeding requiredDateStr and format
        // of "dd/MM/yyyy HH:mm:ss"

        // java.sql.Date sqlDate = new Date(requiredDateDate.getTime());

    }
}

Edit: Question: How to convert this String "10/01/2013 00:00:00" to java.util.Date format?

Ultimately I want to convert this String "10/01/2013 00:00:00" to a java.sql.Date format

spiderman
  • 10,892
  • 12
  • 50
  • 84
  • And what is your question? – Jens Feb 26 '15 at 17:12
  • It's not clear what you're asking here. You say you want a java.util.Date of a specific format, but java.util.Date isn't in any format at all; it's just a specific instant in time (measured in milliseconds since the epoch) – ComputerDruid Feb 26 '15 at 17:12
  • @Jens edited the post to add the exact question – spiderman Feb 26 '15 at 17:32
  • @ComputerDruid you opened my eyes.. Thank you for that information. Yes, I was trying to get a Date of a specific format which is not possible! – spiderman Feb 26 '15 at 17:41

1 Answers1

1

Use SimpleDateFormat:

SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss") ;
df.parse("10/01/2013 00:00:00");

See the documentation for more Information about the Format.

For your second question see this thread.

Community
  • 1
  • 1
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Thanks for the link for my second qn. First one doesn't help me as it gives a Date of some format, where as I was looking for Date of a particular format. For the first one, ComputerDruid well said. If you can append that to your answer, I can accept yours and close this. Thank you for helping me – spiderman Feb 26 '15 at 17:42
  • @spiderman it gives you the date for the Format you asking for?! – Jens Feb 26 '15 at 17:44
  • No it doesn't. `java.util.Date` will not. thats what he was explaining – spiderman Feb 26 '15 at 17:49
  • I was referring to this comment of @ComputerDruid "java.util.Date isn't in any format at all; it's just a specific instant in time (measured in milliseconds since the epoch) " – spiderman Feb 26 '15 at 19:27
  • @spiderman yes i know. I am not sure what hat this too du with your question? `Question: How to convert this String "10/01/2013 00:00:00" to java.util.Date format?` – Jens Feb 26 '15 at 19:29
  • Sorry for that, my question title gives more clarity - How to convert this String "10/01/2013 00:00:00" to java.util.Date of the format 'dd/mm/yyyy HH:mm:ss' . If I just do a parse on the dateString, I get a java.util.Date of some format, not exactly in 'dd/mm/yyyy HH:mm:ss' – spiderman Feb 26 '15 at 19:33