tl;dr
ZonedDateTime.parse( // Parse string into a date + time-of-day + time zone.
… , // Your input string.
DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss zzz uuuu" , Locale.US ) // Specify `Locale` to determine human language and cultural norms in parsing and translating the text.
)
.toLocalDate() // Extract the date-only portion of the `ZonedDateTime` object.
.isEqual(
LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) // Get current date as seen by people of a certain region (time zone).
)
java.time
The Answer by aUserHimself is correct in suggesting the use of jsoup library. But the example code is ill-advised in other ways, making these few mistakes:
- Using troublesome legacy date-time classes. Those classes are now supplanted by the java.time classes.
- Assumes the day starts at 00:00:00. Not true for all dates in all time zones. Anomalies such as Daylight Saving Time (DST) mean the day may start at a time such as 01:00:00.
- Ignoring the issue of
Locale
, which determines the human language used in parsing the text of the name of month, name of day-of-week, etc. The Locale
also determines the expected punctuation and other cultural norms.
- Ignores the crucial issue of time zone in determining the current date.
Example code.
String input = … ;
Locale locale = Locale.US ;
DateTimeFormatter f = DateTimeFormatter.ofPattern( "EEE MMM d HH:mm:ss zzz uuuu" , locale ) ;
ZonedDateTime zdt = ZonedDateTime.parse( input , f ) ;
LocalDate ld = zdt.toLocalDate() ;
Compare to today's date. Must specify the expected/desired time zone. For any given moment, the date varies around the world by zone. A new day dawns earlier in India than in Canada, for example.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
Boolean isSameDate = ld.isEqual( today ) ;
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.
With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or 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.