0

I have String

String ackwardDate="2012-10-30T19:14:58";

i need to convert to date format.then tried like this

     ackwardDate=ackwardDate.replace("T", " ");
           SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");


            try {

                Date date = formatter.parse(ackwardDate);
                System.out.println(date);
                System.out.println(formatter.format(date));

            } catch (ParseException e) {
                e.printStackTrace();
            }

it working fine. Is there any other method?

Jens
  • 67,715
  • 15
  • 98
  • 113
Sreekanth
  • 407
  • 4
  • 8
  • 20

4 Answers4

2

You can include the T in your date format. And also looking at your ackwardDate string, you need to use HH instead of hh as it has the 24-hour format and that is represented by H.

String ackwardDate="2012-10-30T19:14:58";
// ackwardDate=ackwardDate.replace("T", " "); // Not needed anymore
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

SimpleDateFormat docs should help you understand more about the patterns available.

Rahul
  • 44,383
  • 11
  • 84
  • 103
1

You can use:

new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss")

and do not replace the character 'T'

Jens
  • 67,715
  • 15
  • 98
  • 113
1

I think you don't want T with your resulted date and time so only you used replace() method. Replace T with space is simple and good solution only instead you can try with this if you don't want to use ackwardDate=ackwardDate.replace("T", " ") instead you can get date object(Date date = formatter.parse(str)) for yyyy-MM-ddTHH:mm:ss and from that date object you can get yyyy-MM-dd separately and HH:mm:ss or hh:mma. Use date object as your requirement.

        String str = "2012-10-30T19:14:58"; 
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat formatterDate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatterTime = new SimpleDateFormat("HH:mm:ss");
        try {
            Date date = formatter.parse(str);
            System.out.println(date);
            System.out.println("Complete Date: "+formatter.format(date));
            System.out.println("Date: "+formatterDate.format(date));
            System.out.println("Time: "+formatterTime.format(date));
            System.out.println("Date And Time: "+formatterTime.format(date)+" "+formatterTime.format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
SathishKumar
  • 1,644
  • 1
  • 14
  • 28
1

If you know that always you will have the same format of date string you are having, then keep using SimpleDateFormat otherwise it is better to split your string and create using GregorianCalendar

`String ackwardDate="2012-10-30T19:14:58";

    SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd'T'hh:mm:ss");
    try {
        Date dt=df.parse(ackwardDate);
        System.out.println(dt);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }`