1

apart from the inbuilt functions, can we use any simple formulas to calculate the start day of the month given month and year as inputs??

1
  • I certainly recommend you use the in-built methods. Nowadays that would mean java.time, the modern Java date and time API. Avoid Calendar, it’s a poorly designed and long outdated class. Commented May 30, 2021 at 12:06

4 Answers 4

3

Yes - use the Calendar object:

Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2009) cal.set(Calendar.MONTH, Calendar.JUNE) //0-based so that Calendar.JANUARY is 0 cal.set(Calendar.DAY_OF_MONTH, 1) 

Then:

cal.get(Calendar.DAY_OF_WEEK); //is Calendar.MONDAY 

So you could easily compose a method for this:

/** takes a 1-based month so that Jjanuary is 1 */ public int getDayAtStart(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year) cal.set(Calendar.MONTH, month - 1); cal.set(Calendar.DAY_OF_MONTH, 1); return cal.get(Calendar.DAY_OF_WEEK); } 
Sign up to request clarification or add additional context in comments.

Comments

3

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Also, quoted below is a notice at the Home Page of Joda-Time:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Solution using java.time, the modern API:

import static java.lang.System.out; import java.time.DayOfWeek; import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.time.format.TextStyle; import java.util.Locale; public class Main { public static void main(String[] args) { DayOfWeek dow = getFirstDayOfMonth(2021, 5); out.println(dow); // SATURDAY // As weekday number out.println(dow.getValue()); // 6 // Some predefined styles out.println(dow.getDisplayName(TextStyle.FULL, Locale.ENGLISH)); // Saturday out.println(dow.getDisplayName(TextStyle.SHORT, Locale.ENGLISH)); // Sat out.println(dow.getDisplayName(TextStyle.NARROW, Locale.ENGLISH)); // S // Formatting using DateTimeFormatter out.println(DateTimeFormatter.ofPattern("EEEE", Locale.ENGLISH).format(dow)); // Saturday out.println(DateTimeFormatter.ofPattern("EEE", Locale.ENGLISH).format(dow)); // Sat out.println(DateTimeFormatter.ofPattern("EEEEE", Locale.ENGLISH).format(dow)); // S } private static DayOfWeek getFirstDayOfMonth(int year, int month) { return YearMonth.of(year, month) .atDay(1) .getDayOfWeek(); } } 

ONLINE DEMO

Note: If your requirement is to find the same for a specific timezone, use LocalDate#atStartOfDay(ZoneId) as shown below:

import static java.lang.System.out; import java.time.DayOfWeek; import java.time.YearMonth; import java.time.ZoneId; public class Main { public static void main(String[] args) { DayOfWeek dow = getFirstDayOfMonth(2021, 5, ZoneId.of("America/Los_Angeles")); out.println(dow); // SATURDAY } private static DayOfWeek getFirstDayOfMonth(int year, int month, ZoneId zoneId) { return YearMonth.of(year, month) .atDay(1) .atStartOfDay(zoneId) .getDayOfWeek(); } } 

ONLINE DEMO

Learn more about java.time, the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Comments

2

As ever, I'd recommend using Joda Time. I dare say java.util.Calendar will work for this, but I prefer to use Joda Time for all date and time calculations wherever possible. The built-in APIs are horrible. Joda makes this very easy indeed. (Unless specified, LocalDate assumes an ISOChronology - I expect that's what you want, but you can specify a different one if you need to.)

import org.joda.time.*; public class Test { // Just for the sake of a simple test program! public static void main(String[] args) throws Exception { // Prints 1 (Monday) System.out.println(getFirstDayOfMonth(2009, 6)); } /** Returns 1 (Monday) to 7 (Sunday) */ private static int getFirstDayOfMonth(int year, int month) { LocalDate date = new LocalDate(year, month, 1); return date.getDayOfWeek(); } } 

Or if you just need to do this in one place, you can mash it into a single line if you want:

int day = new LocalDate(year, month, 1).getDayOfWeek(); 

It's hard to see how it could be much simpler than that :)

4 Comments

Not much point in learning Joda time now that JSR 310 is about to hit the shelves?
I'll believe it when I see it actually released - and when everyone is actually using Java 7. I don't expect everywhere to jump to it the instant it's shipped. Besides with the same designer I expect it'll be similar enough that time spent learning Joda Time now won't be wasted anyway.
If you look at it, it feels quite different to JODA
I bet it feels closer to Joda than it does to Date/Calendar... and anyway, in the meantime I'd much rather use a decent API with an appropriate separation of dates, times, instants etc than calendar. I've been doing a lot of date/time work over the last year, and I wouldn't go back to Date/Calendar for anything...
1

construct a calendar (cal) with the given month and year, and day 1, and cal.get(Calendar.DAY_OF_WEEK)... sorry, don't think there are any builtins just for that task

Comments