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.