0

My date string is: "Wed Oct 19 14:34:26 BRST 2016" and I'm trying to parse it to "dd/MM/yyyy", but I'm getting the following exception:

java.text.ParseException: Unparseable date: "Wed Oct 19 14:34:26 BRST 2016" (at offset 20)

the method

public String dataText(int lastintervalo) {

        Date mDate = new Date();
        String dt = mDate.toString();
        SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                Locale.getDefault());
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(sdf.parse(dt));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        sdf.applyPattern("dd/MM/yyyy");

        c.add(Calendar.DATE, lastintervalo);
        return sdf.format(c.getTime());

    }

I already searched on google and stackoverflow questions, but nothing seems to work

AND4011002849
  • 1,994
  • 6
  • 31
  • 75

3 Answers3

2

Since the error message is complaining about offset 20, which is the BRST value, it seems that it cannot resolve the time zone.

Please try this code, that should ensure that the Brazilian time zone is recognized:

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
System.out.println(sdf.parse("Wed Oct 19 14:34:26 BRST 2016"));

Since I'm in Eastern US, that prints this for me:

Wed Oct 19 12:34:26 EDT 2016
Andreas
  • 138,167
  • 8
  • 112
  • 195
1

tl;dr

ZonedDateTime.parse ( 
    "Wed Oct 19 14:34:26 BRST 2016" , 
    DateTimeFormatter.ofPattern ( "EE MMM dd HH:mm:ss z uuuu" )
)
.toLocalDate()
.format( 
    DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT)
                     .withLocale( Locale.UK ) 
)

19/10/16

java.time

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

Use real time zone

Never use the 3-4 letter abbreviation such as BRST or EST or IST as they are not true time zones, not standardized, and not even unique(!).

Specify a proper time zone name in the format of continent/region. For example, America/Sao_Paulo.

So while the code below works in this particular case, I do not recommend exchanging data such as your input. If you have influence over the data source, change to using standard ISO 8601 formats for data exchange of date-time values.

2016-10-19T14:34:26-02:00

Even better, exchange strings as created by ZonedDateTime that extend ISO 8601 format by appending the time zone name in square brackets.

2016-10-19T14:34:26-02:00[America/Sao_Paulo]

Best of all, usually, is to convert such values to UTC before exchanging data. In java.time, call toInstant().toString() to do this. Generally best to work in UTC, applying a time zone only where required such as presentation to the user.

2016-10-19T16:34:26Z

Example code

String input = "Wed Oct 19 14:34:26 BRST 2016";
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "EE MMM dd HH:mm:ss z uuuu" );
ZonedDateTime zdt = ZonedDateTime.parse ( input , f );

zdt.toString(): 2016-10-19T14:34:26-02:00[America/Sao_Paulo]

To see the same moment in UTC, extract a Instant.

Instant instant = zdt.toInstant();

instant.toString(): 2016-10-19T16:34:26Z

The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate ld = zdt.toLocalDate();

ld.toString(): 2016-10-19

To generate strings in other formats, search Stack Overflow for DateTimeFormatter class. Generally best to let java.time localize automatically.

To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, and such.

Example:

Locale l = Locale.CANADA_FRENCH ;  // Or Locale.US or Locale.ITALY etc.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT).withLocale( l );
String output = ld.format( f );

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
  • 218,480
  • 72
  • 657
  • 915
0

Try this..It may work

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",
                    Locale.ENGLISH);
Keval Pithva
  • 560
  • 1
  • 5
  • 20