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 .

Pawan
  • 31,545
  • 102
  • 256
  • 434
  • http://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html#parse-java.lang.String- – Dmitry Ginzburg Dec 09 '14 at 09:25
  • 1
    possible duplicate of [Java string to date conversion](http://stackoverflow.com/questions/4216745/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. – l4mpi Dec 09 '14 at 09:28

4 Answers4

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.

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
3

Instead of

String strDate = sdf.format(input);

use

String strDate = sdf.parse(input);

Also see this question

Community
  • 1
  • 1
Liam de Haas
  • 1,258
  • 3
  • 18
  • 39
  • Thanks , it returns me Sun Dec 30 02:18:38 IST 8 How can i get 09-12-2014 02:18:38 – Pawan Dec 09 '14 at 09:24
  • @PreethiJain Read [the docs](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) 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. – Duncan Jones Dec 09 '14 at 09:25
  • 1
    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](http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html)) – Liam de Haas Dec 09 '14 at 09:27
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);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
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);
Brian
  • 23
  • 5