Search code examples
androidsimpledateformatdate-formatdate-formatting

How to pass date format Response to SimpleDateFormat?


I am getting Date Formate Response like 11:10 AM Thursday - March, 02 2017.

i have tried like this

 String time = 11:10 AM  Thursday - March, 02 2017
   try {


 Date cDate = new SimpleDateFormat("hh:mm aa EEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);
                    System.out.println(cDate);

                    Calendar cal = Calendar.getInstance();
                    cal.setTime(cDate);
                    cal.setTimeZone(TimeZone.getTimeZone("UTC"));

                    String monthName = new SimpleDateFormat("MMMM", Locale.ENGLISH).format(cal.getTime());
                    String day = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(cal.getTime());
                    int year = Calendar.getInstance().get(Calendar.YEAR);
                    String date = new SimpleDateFormat("dd", Locale.ENGLISH).format(cal.getTime());
                    String hour = new SimpleDateFormat("hh", Locale.ENGLISH).format(cal.getTime());
                    String min = new SimpleDateFormat("mm", Locale.ENGLISH).format(cal.getTime());
                    String am_pm = new SimpleDateFormat("a", Locale.ENGLISH).format(cal.getTime());

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

But it goes in exception saying:

Unparseable date: "11:10 AM Thursday - March, 02 2017" (at offset 9)

So how to pass Date Format like this String?


Solution

  • Well, you just missed one small thing.

    you are using like :

    Date cDate = new SimpleDateFormat("hh:mm aa EEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);
    

    instead, you need to use like this

    Date cDate = new SimpleDateFormat("hh:mm aa  EEE - MMM, dd yyyy", Locale.ENGLISH).parse(time);
    

    You missed the one extra space of Thursday