-1

I'm using this code to parse a Date, but in this case my date String is as follows:

Fri, 14 Oct 2016 13:56:15 +0200

(spanish Spain)

Don't know offset 8 and, or how to fix that parse error.

PD: I've tried too this solution with no success.

My code....

public String convertDate(String d) {

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy hh:mm", new Locale("es"));
try
{

    Date date = simpleDateFormat.parse(d);
    return simpleDateFormat.format(date);
    //System.out.println("date : "+simpleDateFormat.format(date));
}
catch (ParseException ex)
{
    System.out.println("Exception "+ex);
}
return "Error en la fecha";

}

Logat

I/System.out: Exception java.text.ParseException: Unparseable date: "Fri, 14 Oct 2016 13:56:15 +0200" (at offset 0)
I/System.out: Exception java.text.ParseException: Unparseable date: "Thu, 13 Oct 2016 11:48:48 +0200" (at offset 0)
I/System.out: Exception java.text.ParseException: Unparseable date: "Wed, 12 Oct 2016 13:57:02 +0200" (at offset 0)
I/System.out: Exception java.text.ParseException: Unparseable date: "Thu, 13 Oct 2016 21:06:31 +0200" (at offset 0)

PD2: Using Android Studio

Community
  • 1
  • 1
Mark
  • 684
  • 2
  • 9
  • 25

1 Answers1

2

Locale

Specify Locale to match the human language of your input string. In this case, English rather than Spanish, Locale.English.

If omitted, your JVM’s current default locale is applied implicitly. Better to be explicit. Always specify your desired/expected locale. Making the locale argument optional is a flaw in the API design in my opinion.

RFC 1123 & 822

Your input string happens to comply with the standard formats of RFC 1123 and RFC 822.

This format is predefined by DateTimeFormatter.RFC_1123_DATE_TIME constant. So no need to define a formatting pattern.

ZonedDateTime

You are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.

Parse your input string as a ZonedDateTime using the predefined formatting pattern.

ZonedDateTime zdt = ZonedDateTime.parse( 
    "Fri, 14 Oct 2016 13:56:15 +0200" , 
    DateTimeFormatter.RFC_1123_DATE_TIME 
);

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Sorry, I didn't explain I'm using Android Studio, so there is no ZonedDateTime – Mark Oct 15 '16 at 15:44
  • 2
    @Mark Add the [*ThreeTenABP*](https://github.com/JakeWharton/ThreeTenABP) library to your project, an Android adaptation of the [*ThreeTen-Backport*](http://www.threeten.org/threetenbp/) project. Much of the java.time functionality is back-ported to Java 6 & 7. Well worth the bother as the legacy date-time classes are an awful mess. – Basil Bourque Oct 15 '16 at 15:47