How to determine day of week by passing specific date?
To obtain the day of the week from a given date in Java, you can make use of the LocalDate
class along with the DayOfWeek
enum:
The LocalDate.of(year, month, day)
method generates a LocalDate
instance, and the getDayOfWeek()
method analyzes that instance, returning the DayOfWeek
enum.
Parsing diverse date formats
Sometimes, you might need to work with different date formats. In such cases, DateTimeFormatter
comes to the rescue:
The above example shows flagrantly how we're defining yet another plot twist on the date format side of things.
Compatibility and Time Zone
The ThreeTen-Backport library enables you to use the java.time
package in Java 6 and 7, and even on Android via the ThreeTenABP extension.
When Mars’ time zones come into play, you can opt for ZonedDateTime
to get accurate localized day data:
Formatter and Locale tactics
Locale warriors
How about displaying the same date in the language of Cervantes? Here's how:
Complex pattern handling
Be cautious with Locale.getDefault()
. Define explicit patterns for DateTimeFormatter
to tackle unexpected errors. Iso-formatted date? No problem:
Time-traveling with SimpleDateFormat
For applications stuck in the past, you can use the java.text.SimpleDateFormat
:
Was this article helpful?