Determining the Day of the Week from a Given Date

In this tutorial, we will explore how to determine the day of the week from a given date. This can be achieved using various methods and libraries, including Java’s built-in java.util.Calendar class, the SimpleDateFormat class, Joda-Time library, and the java.time package introduced in Java 8.

Using Java’s Built-in Classes

Before Java 8, you could use the java.util.Calendar class to determine the day of the week. Here is an example:

import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) throws Exception {
        Calendar c = Calendar.getInstance();
        c.set(2010, 1, 23); // Note: months are 0-based in Java
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        System.out.println("Day of week: " + dayOfWeek);

        SimpleDateFormat sdf = new SimpleDateFormat("EEE");
        String dayName = sdf.format(c.getTime());
        System.out.println("Day name: " + dayName);
    }
}

However, using Calendar and SimpleDateFormat can be cumbersome and error-prone due to their mutable nature and the complexities of date and time calculations.

Using Joda-Time

Joda-Time is a popular library for working with dates and times in Java. Although it’s now in maintenance mode, it’s still widely used. Here’s how you could use Joda-Time to find the day of the week:

import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern("d/M/yyyy");
        DateTime dt = formatter.parseDateTime("23/2/2010");
        int dayOfWeek = dt.getDayOfWeek();
        System.out.println("Day of week: " + dayOfWeek);

        String dayName = dt.dayOfWeek().getAsShortText();
        System.out.println("Day name: " + dayName);
    }
}

Using Java 8’s java.time Package

Java 8 introduced the java.time package, which provides a comprehensive model for dates and times. It includes classes like LocalDate, LocalTime, LocalDateTime, ZonedDateTime, etc., and offers a more functional and thread-safe approach compared to the older date/time APIs.

Here’s how you can use java.time to determine the day of the week:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/M/yyyy");
        LocalDate date = LocalDate.parse("23/2/2010", formatter);
        int dayOfWeek = date.getDayOfWeek().getValue();
        System.out.println("Day of week: " + dayOfWeek);

        String dayName = date.getDayOfWeek().getDisplayName(TextStyle.SHORT, Locale.US);
        System.out.println("Day name: " + dayName);
    }
}

Conclusion

Determining the day of the week from a given date can be accomplished through various methods and libraries in Java. While java.util.Calendar and Joda-Time are still viable options, the java.time package introduced in Java 8 offers a more modern, functional, and thread-safe approach to working with dates and times.

Advice

  • For new projects or when updating existing code, consider using the java.time package for its clarity, safety, and performance benefits.
  • Be mindful of the locale when formatting day names to ensure they are correctly represented in different languages and regions.
  • Always prefer immutable date/time objects to avoid unintended modifications.

Additional Resources

Leave a Reply

Your email address will not be published. Required fields are marked *