Explain Codes LogoExplain Codes Logo

How to determine day of week by passing specific date?

java
date-formatting
datetimeformatter
localdate
Alex KataevbyAlex Kataev·Nov 17, 2024
TLDR

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:

import java.time.*; public class DayOfWeekExample { public static void main(String[] args) { LocalDate date = LocalDate.of(2023, 3, 15); //Because who doesn't like pi day? DayOfWeek day = date.getDayOfWeek(); System.out.println(day); // The suspense...oh wait, it's WEDNESDAY! } }

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:

import java.time.*; import java.time.format.DateTimeFormatter; import java.time.format.TextStyle; import java.util.Locale; public class RandomDateFormatExample { public static void main(String[] args) { String dateString = "15-Mar-2023"; // Middle-endian date format, how quaint. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.ENGLISH); LocalDate date = LocalDate.parse(dateString, formatter); DayOfWeek day = date.getDayOfWeek(); String dayName = day.getDisplayName(TextStyle.FULL, Locale.ENGLISH); System.out.println(dayName); // Plot twist: It's still WEDNESDAY! } }

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:

import java.time.*; public class MarsTimeExample { public static void main(String[] args) { ZonedDateTime zonedDate = ZonedDateTime.of(2023, 3, 15, 0, 0, 0, 0, ZoneId.of("America/New_York")); DayOfWeek day = zonedDate.getDayOfWeek(); System.out.println(day); // The mars rover says it's WEDNESDAY our time!! } }

Formatter and Locale tactics

Locale warriors

How about displaying the same date in the language of Cervantes? Here's how:

String dayInSpanish = day.getDisplayName(TextStyle.FULL, new Locale("es", "ES")); System.out.println(dayInSpanish); // It's...miércoles! (No siesta jokes, promise)

Complex pattern handling

Be cautious with Locale.getDefault(). Define explicit patterns for DateTimeFormatter to tackle unexpected errors. Iso-formatted date? No problem:

String isoDate = "2023-03-15"; DateTimeFormatter isoFormatter = DateTimeFormatter.ISO_DATE; LocalDate isoToLocalDate = LocalDate.parse(isoDate, isoFormatter); System.out.println(isoToLocalDate.getDayOfWeek()); // It's still WEDNESDAY! Déjà vu, anyone?

Time-traveling with SimpleDateFormat

For applications stuck in the past, you can use the java.text.SimpleDateFormat:

import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatExample { public static void main(String[] args) throws Exception { String dateStr = "2023-03-15"; SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date date = formatter.parse(dateStr); formatter.applyPattern("EEEE"); String dayOfWeek = formatter.format(date); System.out.println(dayOfWeek); // No matter how you slice it, it's still WEDNESDAY! } }