3

I'm getting these times from Facebook events. E.g: start_time and it's a string like this:

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

Now I just want the time, like:

18.30

I tried to do it with 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 like I said before.

If I do this method, I get an error:

Unparseable date.

Does anybody know a work around?

rikitikitik
  • 2,414
  • 2
  • 26
  • 37
user3011083
  • 67
  • 2
  • 10

4 Answers4

11

You need two formats: one to parse the date and one to format it

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));

prints

 Saturday, 21 December 2013
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

I'm getting these times from Facebook events. E.g: start_time and it's a string like this:

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

Now I just want the time, like:

18.30

Solution using java.time, the modern date-time API:

OffsetDateTime.parse(
    "2013-12-21T18:30:00+0100", 
    DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssZ")
).toLocalTime()

Description: Your date-time string has a timezone offset of +01:00 hours. java.time API provides you with OffsetDateTime to contain the information of date-time units along with the timezone offset. Using the applicable DateTimeFormatter, parse the string into an OffsetDateTime and then get the LocalTime part of this date-time using OffsetDateTime#toLocalTime.

Demo using java.time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

class Main {
    public static void main(String[] args) {
        System.out.println(OffsetDateTime.parse(
            "2013-12-21T18:30:00+0100", 
            DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssZ")
        ).toLocalTime());
    }
}

Output:

18:30

ONLINE DEMO

Note: You can use y instead of u here but I prefer u to y.

Learn more about the modern Date-Time API from Trail: Date Time.

A note about the legacy API:

The question and the accepted answer use java.util.Date and SimpleDateFormat which was the correct thing to do in 2013. In Mar 2014, the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern date-time API. Since then, it is highly recommended to stop using the legacy date-time API.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

Use something like yyyy-MM-dd'T'HH:mm:ssZ as parsing format instead of EEEE, dd MMMM yyyy.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Substring

If all you want is literally the time component lifted from that string, call the substring method on the String class…

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

String dateTimeStringFromFacebook = "2013-12-21T18:30:00+0100";

// Extract a substring.
String timeSubstring = dateTimeStringFromFacebook.substring( 11, 19 );

DateTime Object

If you want the time converted to a particular time zone, convert the string to a date-time object. Use a formatter to express just the time component.

Here is some example code using the Joda-Time 2.3 library. Avoid the notoriously bad java.util.Date/Calendar classes. Use either Joda-Time or the new java.time.* JSR 310 classes bundled with Java 8.

// From String to DateTime object.
DateTime dateTime = new DateTime( dateTimeStringFromFacebook, DateTimeZone.UTC );

// From DateTime object to String
// Extract just the hours, minutes, seconds.
DateTimeFormatter formatter = DateTimeFormat.forPattern("HH:mm:ss");
String timeFragment_Paris = formatter.withZone( DateTimeZone.forID( "Europe/Paris" ) ).print( dateTime );
String timeFragment_Kolkata = formatter.withZone( DateTimeZone.forID( "Asia/Kolkata" ) ).print( dateTime ); // Formerly known as Calcutta, India.

Dump to console…

System.out.println( "dateTimeStringFromFacebook: "  + dateTimeStringFromFacebook );
System.out.println( "timeSubstring: " + timeSubstring );
System.out.println( "dateTime: "  + dateTime );
System.out.println( "timeFragment_Paris: "  + timeFragment_Paris );
System.out.println( "timeFragment_Kolkata: " + timeFragment_Kolkata + " (Note the 00:30 difference due to +05:30 offset)");

When run…

dateTimeStringFromFacebook: 2013-12-21T18:30:00+0100
timeSubstring: 18:30:00
dateTime: 2013-12-21T17:30:00.000Z
timeFragment_Paris: 18:30:00
timeFragment_Kolkata: 23:00:00 (Note the 00:30 difference due to +05:30 offset)

Think Time Zone

Your question fails to address the question of time zone. Make a habit of always thinking about time zone whenever working with date-time values. If you mean the same time zone, say so explicitly. If you mean the default time zone of the Java environment, say so. If you mean UTC… well, you get the idea.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154