Check date () on monday? Java

Is there a way to check if a java Date object is on Monday? I see that you can with a Calendar object, but a date? I also use US Eastern dates and times if this changes Monday's indexing

+8
java date
source share
4 answers

Something like this will work:

 Calendar cal = Calendar.getInstance(); cal.setTime(theDate); boolean monday = cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY; 
+21
source share

You can use the Calendar object.

Set a date in a calendar object using setTime (date)

Example:

 calObj.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY 

EDIT: As Jon Skeet suggested, you need to set TimeZone on the Calendar object to make sure it is great for the time zone.

+9
source share

The question does not make sense without two additional pieces of information: time zone and calendar system.

The A Date object represents instant time. Most likely, the environment in the Gregorian calendar is in my time zone - but for some people east of me, it is already on Thursday. Other calendar systems may not even have such a Monday concept, etc.

Part of the calendar system is probably not a problem, but you will need to decide which time zone you are interested in.

Then you can create a Calendar object and set both the time zone and the moment presented - or, better, you can use Joda Time , which is a much better date / time API. You still need to think about the same issues, but your code will be more clear.

+5
source share

For these checks, you must use the Calendar object. Date has weak timezone support. At one point in time, this date can be on Monday, and at another time zone it is still Sunday.

+3
source share

All Articles