0

I have an input date as string in format dd/MM/yyyy

Now I want to convert it into Date object having format yyyy-MM-dd

Currently I am doing

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
Date dateObject = null;
  try {
    String date2 = formatter2.format(formatter.parse(date));
    dateObject = formatter2.parse(date2);
  }

input is: "04/02/2013"

But my date object is coming out to be --> Mon Feb 04 00:00:00 IST 2013

Desired output is : 2013-02-04 (Not in string but as a date object)

6
  • Seems you are printing Date directly. Just print date2. Commented Apr 8, 2013 at 9:43
  • I don't think you can choose how a date object is formatted. I don't know why you'd want that either. You can display it in any way you like Commented Apr 8, 2013 at 9:44
  • No.. I need that date object to query the database Commented Apr 8, 2013 at 9:44
  • @Sudhanshu that's his question. Although it makes no sense, he wants to have the Date object to contain the desired string directly. Commented Apr 8, 2013 at 9:45
  • @bhuvan: convert java.util.Date to java.sql.Date and then use a setDate() method of prepared statement. Don't mess with formats! Commented Apr 8, 2013 at 9:45

5 Answers 5

3

A date has no specific output format. When you parse your first date, that's the date. Just store it, and when you need to output it to your desired format, use the second SimpleDateFormat.

Edit:

By "store it", i mean keep a reference to it until you need it represented in your desired String format.

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

1 Comment

Indeed, if you use System.out.println(dateObject), it will just use the default string format, as OP observed. Printing in a custom way requires a formatter.
1

You've already got your formatted date in date2. You needn't parse that again.

String date2 = formatter2.format(formatter.parse(date));
//dateObject = formatter2.parse(date2); // Not needed

System.out.println(date2);

3 Comments

I dont need the string. I need the date object in that format
@bhuvan nope, you don't need a specific format for using Dates with JDBC
as @NilsH mentioned in his answer, java.util.Date has no specific format. You can only format it to a String representation of your choice. But a Date object will always stay that way.
1

I think you're posing the wrong question here. As stated in comments you need this string formatting for querying the database, so, since JDBC works with java.sql.Date objects, be strict to it and convert your java.util.Date object to the JDBC managed type and use it directly (without worrying about formatting).

Try something like this:

String stmt = "select field from table where dateColumn = ?";
PreparedStatement st = connection.prepareStatement(stmt);
st.setDate(1, new java.sql.Date (yourJavaUtilDateObj);
ResultSet rs = st.executeQuery();

Should work on every JDBC driver out there.

4 Comments

Nope in comments OP stated he needs date for jdbc query, wow getting downvotes for actually reading what OP wants... amazing
That would work, if the stored type is actually a date, and not just a date string.
since OP wants to use a Date and says he needs a date, I guess is more than a wild guess ;).
I agree, but since he wanted it in a specific format, I'm still not sure. But it's good advice anyway.
0

Your Date object is correct. Date contains information about the date.

You already have the string with the date with your desired mask:

String date2 = formatter2.format(formatter.parse(date));

try:

System.out.println(date2);

Comments

0

This work for me :

public Timestamp convertStringToTimeStamp(String dateinString){
        final String OLD_FORMAT = "dd/mm/yyyy";
        final String NEW_FORMAT = "yyyy-mm-dd";

        String newDateString;

        SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
        Date d = null;
        try {
            d = sdf.parse(dateinString);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        sdf.applyPattern(NEW_FORMAT);
        newDateString = sdf.format(d);

        return Timestamp.valueOf(newDateString+ " 00:00:00.00");
    }

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.