2

I am trying to parse the date from one format to another format, but getting the parse exception. Please help me out on this issue.

String Orgdate= "2016-11-14T11:12:13";
java.util.Date tardate = null;


SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd/mm/yyyy HH24:MI:SS");//Exception is in this line.
    try {
        targetdateformat  = dateFormat2.parse(Orgdate);
    } catch (ParseException e) {
        e.printStackTrace();
    }

Above code giving me date but different format,i am looking for date like below mention.

('14/11/2016 11:12:13')

Time format is 24 Hrs.

meditat
  • 1,197
  • 14
  • 33
AKULA
  • 21
  • 1

3 Answers3

0

why not just do a simple hardcode to get the format you would like. For example...

            String date = "2016-11-14T11:12:13";

            String newDate = date.charAt(8) + "" + date.charAt(9) + "/" +
                             date.charAt(5) + "" + date.charAt(6) + "/" +
                             date.substring(0, 4) + " " +  date.substring(11, 19);

            System.out.println(newDate);
0

You need to parse to Date then format the date string to convert it into another format

    String Orgdate= "2016-11-14T11:12:13";
    java.util.Date tardate = null;
    SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            tardate = dateFormat2.parse(Orgdate);
            System.out.println(tardate); // Mon Nov 14 11:12:13 UTC 2016
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String formatted = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(tardate);
        System.out.println(formatted); 14/11/2016 11:12:13

If you are using Java8 you can try below

LocalDateTime dateTime = LocalDateTime.parse("2016-11-14T11:12:13", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
String formattedDate = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")); // 14/11/2016 11:12:13
Saravana
  • 12,647
  • 2
  • 39
  • 57
0
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        Date a = sdf.parse("2016-08-12T08:29:47");
        sdf.applyPattern("dd/MM/yyyy HH:mm:ss");
        System.out.println(sdf.toPattern()); // to see if new pattern applied 
        System.out.println(sdf.format(a)); // get output in desired format
Ashish Patil
  • 4,428
  • 1
  • 15
  • 36