Java – How to parse time from a date string retrieved from Facebook

How to parse time from a date string retrieved from Facebook… here is a solution to the problem.

How to parse time from a date string retrieved from Facebook

I get these times from Facebook Activities. For example: start_time is a string like this:

2013-12-21T18:30:00+0100

Now I just want time, like:

18.30

I tried to do this :

SimpleDateFormat formatter = new SimpleDateFormat(" EEEE, dd MMMM yyyy", java.util.Locale.getDefault());
                Date formatted = null;
                try {
                    formatted = formatter.parse(p.getStart_time());
                } catch (ParseException e) {
                     TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String formattedString = formatted.toString();
                txtStart_time.setText(""+formattedString);

p.getStart_time() is a string that gives me the date I said earlier.

If I do this, I get an error :

Unparseable date.

Does anyone know a workaround?

Solution

You need two formats: one for parsing dates and one for formatting

String startTime = "2013-12-21T18:30:00+0100";
SimpleDateFormat incomingFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date date = incomingFormat.parse(startTime);

SimpleDateFormat outgoingFormat = new SimpleDateFormat(" EEEE, dd MMMM yyyy", java.util.Locale.getDefault());

System.out.println(outgoingFormat.format(date));

Print

 Saturday, 21 December 2013

Related Problems and Solutions