tl;dr
For a SQL-standard DATE
column, use the java.time.LocalDate
class with JDBC 4.2 or later.
myPreparedStatement.setObject(
… ,
ZonedDateTime.parse( // Represent a moment in a particular time zone.
"Thu May 24 13:14:41 BRT 2018".replace( "BRT" , "Europe/London" ) ,
DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss z uuuu" , Locale.UK )
)
.toLocalDate() // Extract a date-only value, without time-of-day and without time zone.
)
ISO 8601
I have an variable called data, and it has today's date as in this format: Thu May 24 13:14:41 BRT 201
This is a terrible format. When exchanging date-time values as text, use only the standard ISO 8601 formats. Conveniently, the java.time classes use these formats by default when parsing/generating strings.
BST
= ?? (Brazil Standard Time, British Standard Time, … )
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as BST
or IST
as they are not true time zones, not standardized, and not even unique(!).
The ZonedDateTime
class will try to guess the intended time zone. But you should not be relying on guesses in your code.
String input = "Thu May 24 13:14:41 BRT 2018";
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss z uuuu" , Locale.UK );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );
2018-05-24T13:14:41-03:00[America/Sao_Paulo]
Looks like java.time guessed you meant Brazil Standard Time
by BST
. If you meant some other zone such as British time, replace that BST
string by a proper time zone name.
Specify 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, separators, and such.
String input = "Thu May 24 13:14:41 BRT 2018".replace( "BRT" , "Europe/London" );
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss z uuuu" , Locale.UK );
ZonedDateTime zdt = ZonedDateTime.parse( input , f );
2018-05-24T13:14:41+01:00[Europe/London]
LocalDate
You apparently care about only the date. So extract a date-only object, a LocalDate
.
LocalDate ld = zdt.toLocalDate() ;
Smart objects, not dumb strings
Do not exchange date-time values with your database as text. Instead use JDBC and objects. As of JDBC 4.2 and later, you can directly exchange java.time objects with your database.
myPreparedStatement.setObject( … , ld ) ;
Retrieval.
LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
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
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
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.