0

If I do toString on a Date object, I am getting the output as below

2016-04-13 22:00:01.0

I am doing the below to convert the object to Data again

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd:HH:mm:SS");
Date convertedDate = (Date) formatter.parse(timestamp.toString());

But getting the ParseException.

I am trying to get the output as Date object as below 2016-04-13 22:00:01

3
  • The cast to Date is unnecessary because formatter.parse(...) already returns a Date object. Commented Apr 13, 2016 at 18:27
  • 1
    Voting to close as typo. Commented Apr 13, 2016 at 18:29
  • If i remove the casting also still i am getting the parsing error please help Commented Apr 13, 2016 at 18:34

4 Answers 4

4

This test should cover your example:

First from comment:

    SimpleDateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss zzz yyyy");
    Date convertedDate = formatter.parse("Thu Apr 14 15:24:14 CEST 2016")
    System.out.print(convertedDate);

Second from question:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date convertedDate = formatter.parse("2016-04-13 22:00:01.0");
    System.out.print(convertedDate);

It all depends on input String that you are receiving.

You should not use Date::toString method, use formatter, as you willl have no problem with zones and formats. It's better to have full flow control over your data.

Now if you want to convert it to a " 2016-04-13 22:00:01" format, simply use:

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formatedDate = formatter.format(convertedDate);
Sign up to request clarification or add additional context in comments.

3 Comments

The above didnt work. Date is retruned as Thu Apr 14 00:39:57 IST 2016
This code SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS"); Date convertedDate = formatter.parse("2016-04-13 22:00:01.0"); returns only the below format Wed Apr 13 22:00:01 IST 2016. But my desire output is different System.out.print(convertedDate);
@magesh-balasubramaniam done. But I would advise to use same formatter to convert to and from String. So that you would not have to use more than one.
1

Parse from String with a certain date-format to a Date object:

    String dateString = "2016-04-13 22:00:01.0";
SimpleDateFormat formatterFullMilliseconds = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS" );

Date dateFromDateString = formatterFullMilliseconds.parse( dateString );

System.out.println( dateFromDateString ); // Output: Wed Apr 13 22:00:01 CEST 2016

Parse a java.util.Date object to the string representation you want:

SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
System.out.println( formatter.format( dateFromDateString ) ); // 2016-04-13 22:00:01
System.out.println( formatter.format( new Date() ) ); // 2016-04-14 15:29:04

1 Comment

I have a date Object if i do datObject.toString() i am getting the output i need, but the function that ia m trying to pass the value accepts only Date object. Hence your solution might not work for me
-1
yyyy-MM-dd:HH:mm:SS

should be

yyyy-MM-dd HH:mm:ss.S

2 Comments

Thanks that resolved the exception issue, but i need ouput only in the format 2016-04-13 22:00:01 not 2016-04-13 22:00:01.9
Sorry it didnt work.Date is retruned as Thu Apr 14 00:39:57 IST 2016
-1

This is exactly what you need to do, to escape java.text.ParseException: Unparseable date exception

Date date = new Date();

SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = sp.parse(sp.format(date));
System.out.println(dt.toString());

1 Comment

if I put 2014-10-30 18:10:50, i get java.lang.IllegalArgumentException: Cannot format given Object as a Date

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.