0

I am working on an Android application in which I need to parse my date to get my time. I am getting parsing exception when I am using my below code.

Here is the code:

SimpleDateFormat parseFormat = new SimpleDateFormat("dd MM hh:mm:ss yyyy");
    SimpleDateFormat printFormat = new SimpleDateFormat("h:mm:ss");
    Date date;
    try {   //selectedDateTime = Fri Jan 15 09:30:44 GMT+04:00 2016
        date = parseFormat.parse(selectedDateTime+"");
        System.out.println(printFormat.format(date));
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Here is the exception:

java.text.ParseException: Unparseable date: "Fri Jan 15 09:30:00 GMT+04:00 2016" (at offset 0)

halfer
  • 19,824
  • 17
  • 99
  • 186
Usman Khan
  • 3,739
  • 6
  • 41
  • 89

4 Answers4

0
SimpleDateFormat parserSDF=new SimpleDateFormat("EEE MMM d HH:mm:ss zzz yyyy");

Your formatting is off, try this.

Ludwig S
  • 551
  • 3
  • 8
0

update

    try {
        String dateTest = "Fri Jan 15 09:30:00 GMT+04:00 2016";

        SimpleDateFormat formatter = new SimpleDateFormat(
                "EEE MMM dd hh:mm:ss zzz yyyy");
        java.util.Date d = (java.util.Date) formatter.parse(dateTest);
        System.out.println("" + d);

        SimpleDateFormat print = new SimpleDateFormat("hh:mm:ss");
        System.out.println(print.format(d));

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

output

Fri Jan 15 11:00:00 IST 2016
11:00:00

Following code can help you understand how exactly formatting syntax change for date formatting

    Calendar calendar = Calendar.getInstance();


    System.out.println("Date: \t"+calendar.getTimeInMillis());

    String dateAsText_ = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(""+calendar.getTimeInMillis())));
    Date date = new Date(Long.parseLong(""+calendar.getTimeInMillis()));
    System.out.println(""+dateAsText_);

    SimpleDateFormat ft =  new SimpleDateFormat ("dd-MMM-yyyy");
    System.out.println(""+ft.format(date));

    SimpleDateFormat time_ft =  new SimpleDateFormat ("hh:mm aa");
    System.out.println(""+time_ft.format(date));

Output

Date:   1452766759531
2016-01-14 15:49:19
14-Jan-2016
03:49 PM

enter image description here

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
0

Your Strings are wrong. The correct ones are:

SimpleDateFormat parseFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("HH:mm:ss");
dipdipdip
  • 2,326
  • 1
  • 21
  • 31
0

I guess your initial date format is wrong. Can you share the value of selectedDateTime. I guess it in different format (as per your comment) like Fri Jan 15 09:30:44 GMT+04:00 2016

If so please try this code

SimpleDateFormat parseFormat = new SimpleDateFormat("E MM dd hh:mm:ss z yyyy");
SimpleDateFormat printFormat = new SimpleDateFormat("h:mm:ss");
Date date;
try {   //selectedDateTime = Fri Jan 15 09:30:44 GMT+04:00 2016
    date = parseFormat.parse(selectedDateTime+"");
    System.out.println(printFormat.format(date));
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Mobile Apps Expert
  • 1,028
  • 1
  • 8
  • 11