Reputation: 255
At the moment, I'm creating a java schedule app and I was wondering how to find the current day of the week while using the Calendar class to get the first day of the week.
Here is the method I'm using in a class in order to set up a week schedule
public static String getSunday() {
SimpleDateFormat d = new SimpleDateFormat(dayDate);
Calendar specific = cal;
specific.add(Calendar.DAY_OF_YEAR, (cal.getFirstDayOfWeek() - ??));
return d.format(specific.getTime());
}
Upvotes: 8
Views: 9369
Reputation: 340118
how to find the current day of the week
DayOfWeek.from( LocalDate.now() )
set up a week schedule
LocalDate sunday =
LocalDate.now()
.with( TemporalAdjusters.nextOrSame( DayOfWeek.SUNDAY ) ) ;
LocalDate monday = sunday.plusDays( 1 ) ;
LocalDate tuesday = monday.plusDays( 1 ) ;
…
The Question and other Answer both use outmoded classes.
The old date-time classes are poorly designed, confusing, and troublesome. Avoid them.
In Java 8 and later, we use the built-in java.time framework. Much of the functionality is back-ported to Java 6 & 7 (ThreeTen-Backport), and further adapted to Android (ThreeTenABP).
An Instant
represents the current moment in UTC with a resolution up to nanoseconds.
Instant instant = Instant.now();
To get the day-of-week we must determine the date. Time zone is crucial in determining the date as the date can vary around the globe for any given moment. For example, a few minutes after midnight in Paris is a new day, while in Montréal it is still “yesterday”.
Use ZoneId
to represent a time zone. Ask for a proper time zone name in format of continent/region
. Never use the 3-4 letter abbreviations commonly seen in mainstream media as they are not true time zones, not standardized, and are not even unique(!).
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
Use the handy DayOfWeek
enum to determine the day of the week.
DayOfWeek dow = DayOfWeek.from( zdt );
You may interrogate the DayOfWeek
object.
You may ask for a number from 1 to 7 where 1 is Monday and 7 is Sunday per the ISO 8601 standard. But do not pass this number around your code; instead pass around DayOfWeek
objects to enjoy the benefits of type-safety and guaranteed valid values.
int dowNumber = dow.getValue();
You may want text, the name of the day of the week. Let java.time automatically translate the name into a human language. A Locale
species which human language, and also specifies the cultural norms to use such as how to abbreviate and capitalize. The TextStyle
enum indicates how long a name you want.
Locale locale = Locale.CANADA_FRENCH;
String output = dow.getDisplayName( TextStyle.FULL , locale );
To get the start of the week, ask for the previous Monday (or Sunday, whatever), or stick with today if it is a Monday. Use a TemporalAdjuster
implementation found in TemporalAdjusters
class.
LocalDate startOfWeek = zdt.toLocalDate().with( TemporalAdjusters.previousOrSame( DayOfWeek.MONDAY ) );
If you need a date-time rather than date-only, ask java.time to get the first moment of the day (not always the time 00:00:00
).
ZonedDateTime zdtWeekStart = startOfWeek.atStartOfDay( zoneId );
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
Upvotes: 5
Reputation: 413996
You can get the current day of the week by calling get()
with Calendar.DAY_OF_WEEK
public int getTodaysDayOfWeek() {
final Calendar c = Calendar.getInstance();
return c.get(Calendar.DAY_OF_WEEK);
}
Also, while I'm not sure I understand exactly what you're trying to do, it looks fishy to me. (What's cal
for example?)
Upvotes: 8