1

I'm trying to parse the following string to a Date object:

String str = "04/15/2014 10:30:24"

I'm using SimpleDateFormat :

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
java.util.Date orderDate = sdf.parse(str);
java.sql.Date orderSqlDate = new java.sql.Date(orderDate.getTime());

but orderSqlDate always returned: 04/15/2014 00:00:00

how to use SimpleDateFormat in java exactly?

8
  • 2
    Works fine for me. How are you using orderDate? Commented Apr 15, 2014 at 4:44
  • yeah it perfectly right ... and working just fine Commented Apr 15, 2014 at 4:51
  • ITs working whats prob with that? and SFD use to format date. Commented Apr 15, 2014 at 5:11
  • Show us what you do. Where do you see that the hours/minutes/seconds are lost? Commented Apr 15, 2014 at 15:11
  • after parse orderDate, I will cast it to java.sql.Date: new java.sql.Date(orderDate.getTime()). The output of System.out.print(orderDate.getTime()) is 04/15/2014 00:00:00 Commented Apr 15, 2014 at 15:18

4 Answers 4

2

The java.sql.Date javadoc states

To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.

If you're going to use java.sql.Date, there's no way around this.

Sign up to request clarification or add additional context in comments.

2 Comments

@hoangkianh You can accept an answer by checking the checkmark next to the score. Do this to indicate you are satisfied with the answer provided.
@hoangkianh Welcome to the site!
2

You are also doing correct.

But to get the result in the format you want, you need to use .format("/your format/") method after parsing the string.

String date = "15/12/2014 10:42:24";

SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

Date parseDate = dateParser.parse(date);

formatter.format(parseDate) // this will change format of date as you want.

Comments

1

I don't think the way you parse is wrong. Are you sure you print orderDate right ?

The following code demonstrates both parsing and formatting (printing).

public static void main(String[] args) {
    String format = "MM/dd/yyyy HH:mm:ss";

    try {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date orderDate = new SimpleDateFormat(format).parse("04/15/2014 10:30:24");
        System.out.println(sdf.format(orderDate));
    } catch (Exception e) {
        e.printStackTrace();
    }

}

1 Comment

This should be a comment.
1

Provide Locale in the SimpleDateFormat constructor, otherwise parsing might be dependant on your local settings:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ROOT);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.