4

I recive a date in this format

2014-12-09 02:18:38

which i need to convert it to

09-12-2014 02:18:38

I tried converting this way

import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TestDate {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");
        String input = "2014-12-09 02:18:38";
        String strDate = sdf.format(input);
        System.out.println(strDate);
    }
}

But i am getting this exception during Runtime

Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date
    at java.text.DateFormat.format(DateFormat.java:301)
    at java.text.Format.format(Format.java:157)
    at TestDate.main(TestDate.java:15)

Could anybody please help me how to resolve this .

2
  • docs.oracle.com/javase/8/docs/api/java/text/… Commented Dec 9, 2014 at 9:25
  • 1
    possible duplicate of Java string to date conversion. Also, you should be able to solve trivial issues such as this one by yourself by reading the documentation and googling for a few minutes. SO is not a code-writing service and you shouldn't use it for every little question you have; please do some research on your own. Commented Dec 9, 2014 at 9:28

4 Answers 4

9

Try this

public static void main(String[] args) throws ParseException {
    SimpleDateFormat sdfIn = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdfOut = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    String input = "2014-12-09 02:18:38";
    Date date = sdfIn.parse(input);

    System.out.println(sdfOut.format(date));
}

Also, note that m is for minutes, while M is for months.

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

3 Comments

@PreethiJain This is a solution but I still strongly recommend to read the docs so you'll understand what you are doing
The output i am getitng is 29-12-2014 02:18:38 , why 29 is coming in place of 09 ?
@PreethiJain I have corrected my answer, yyyy should be used instead of YYYY.
3

Instead of

String strDate = sdf.format(input);

use

String strDate = sdf.parse(input);

Also see this question

3 Comments

Thanks , it returns me Sun Dec 30 02:18:38 IST 8 How can i get 09-12-2014 02:18:38
@PreethiJain Read the docs carefully and note the difference between m and M. In fact - look at your pattern in general, it has the years and day the wrong way around.
If you look at the question I linked there's an extensive explenation, also read the docs on the subject (they can be found here)
0
    SimpleDateFormat sdf = new SimpleDateFormat("dd-mm-YYYY HH:mm:ss");

    // String to Date:
    String input = "2014-12-09 02:18:38";
    Date date = sdf.parse(input);

    // Date to String:
    String strDate = sdf.format(date);
    System.out.println(strDate);

Comments

0

SimpleDateFormat in can be used to format a Date object.

First you have to parse your string to a Data object using format on the SimpleDateFormat you declared. After that you can output it using a second SimpleDataFromat to a string you want.

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String input = "2014-12-09 02:18:38";
    Date dt = sdf.parse(input);

    SimpleDateFormat sdf2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");        
    String strDate = sdf2.format(dt);
    System.out.println(strDate);

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.