Determine to day of the week

From CodeCodex

Implementations[edit]

Java[edit]

For the day of the week for today :

GregorianCalendar newCal = new GregorianCalendar( );
int day = newCal.get( Calendar.DAY_OF_WEEK );

For the day of the week for any date :

Calendar newCal = new GregorianCalendar();
newCal.set(1997, 2, 1, 0, 0, 0);
// BUG fix in Calendar class!
newCal.setTime(newCal.getTime());    
int day = newCal.get(Calendar.DAY_OF_WEEK);
/*  
 also available :
   newCal.get( Calendar.DAY_OF_MONTH )
   newCal.get( Calendar.DAY_OF_WEEK_IN_MONTH )
   newCal.get( Calendar.DAY_OF_YEAR )      
   newCal.get( Calendar.DATE )           
*/

Ruby[edit]

cal = Time.now
wday = cal.wday         # 0:sunday - 6:saturday

date = Time.mktime(1997, 2, 1, 0, 0, 0)
date.wday
date.month              # the month of the year (1..12) 
date.mday               # the day of the month (1..n)
date.yday               # an integer representing the day of the year, 1..366
date.year