LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Quick Java question (https://www.linuxquestions.org/questions/programming-9/quick-java-question-138423/)

Nylex 01-25-2004 09:49 AM

Quick Java question
 
I have a GregorianCalendar object and want to find out which day the first of a given month is. How do I go about this? I'm really stuck.

Thanx for any help.

eric.r.turner 01-25-2004 06:24 PM

Is this what you're looking for?

Code:

/*
 * CalendarTest.java
 *
 * Created on January 25, 2004, 5:09 PM
 */

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 *
 * @author  eric.r.turner
 */
public class CalendarTest {

    /*
    *  Prints which weekday the first day of the month falls on.
    */
    public void printFirstDayOfMonth( int year , int month ) {

        GregorianCalendar calendar = new GregorianCalendar( year , month , 1 );
       
        switch ( calendar.get( Calendar.DAY_OF_WEEK ) ) {
            case Calendar.SUNDAY :
                    System.out.println( "Sunday" );
                    break;
            case Calendar.MONDAY :
                    System.out.println( "Monday" );
                    break;
            case Calendar.TUESDAY :
                    System.out.println( "Tuesday" );
                    break;
            case Calendar.WEDNESDAY :
                    System.out.println( "Wednesday" );
                    break;
            case Calendar.THURSDAY :
                    System.out.println( "Thursday" );
                    break;
            case Calendar.FRIDAY :
                    System.out.println( "Friday" );
                    break;
            case Calendar.SATURDAY :
                    System.out.println( "Saturday" );
                    break;
        }
    }

    public static void main(String[] args) {
        CalendarTest ct = new CalendarTest();
        ct.printFirstDayOfMonth( 2004 , Calendar.FEBRUARY );
    }   
}


Nylex 01-26-2004 12:44 PM

Yes. Bloody hell, I'm thick.

Thank you very much :).


All times are GMT -5. The time now is 09:50 PM.