Reputation:
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??
Upvotes: 1
Views: 2050
Reputation: 79560
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();
}
}
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();
}
}
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.
Upvotes: 3
Reputation: 1503290
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 :)
Upvotes: 2
Reputation: 449
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
Upvotes: 1
Reputation: 134330
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);
}
Upvotes: 3