Calendar

Display Month of year using Java Calendar

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

Add Comment

  • I have to make a code that lets the user in put a number equivalent to a month and a year..
    then it will print the month together with its days.. it will also print the previous and next month to it.. i still couldn’t do it with all my efforts.. here’s my code, yet..

    import java.util.GregorianCalendar;

    class Calendar{

    public static void main(String[] args) {

    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(year, motnh-1, 1);
    int x = calendar.get(GregoriaanCalendar.DAY_OF_WEEK);
    int year = Integer.parseInt(args[1]);
    int month = Integer.parseInt(args[0]);
    int numDays = 0;

    switch (month) {
    case 1: System.out.println(“January” + year);
    case 3: System.out.println(“March” + year);
    case 5: System.out.println(“May” + year);
    case 7: System.out.println(“July” + year);
    case 8: System.out.println(“August” + year);
    case 10: System.out.println(“October” + year);
    case 12: System.out.println(“December” + year);
    numDays = 31;
    break;
    case 4: System.out.println(“April” + year);
    case 6: System.out.println(“June” + year);
    case 9: System.out.println(“September” + year);
    case 11: System.out.println(“November” + year);
    numDays = 30;
    case 2: System.out.println(“February” + year);
    if ( ((year % 4 == 0) && !(year & 100 == 0)) || (year % 400 == 0) )
    numdays = 29;
    else
    numDays = 28;
    break;
    default : System.out.println(“Invalid Month.”);
    break;

    System.out.prinln(“SunMonTueWedThuFriSat”);

    is it wrong? what part?? please guide me..
    thanks and more power..

  • System.out.print(“Current month is : ” +
    now.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US)
    );

    See my comment to “Display Day of Week using Java Calendar”

Sponsors

Facebook Fans