Sebastian
Sebastian

Reputation: 29

Java - Months and days?

First of, I have been searching to find anything close to my question but I couldn't find anything. I am not a very good programmer, I have just started to play with it a little, and my interest for it is growing a lot.

I was thinking if I could make a basic program with basic and easy understandable language, to a month and days "calculator".

Like if I have a sysout print which says: Write month number, and I'll type in 11, and then write a day number in the month and someone writes 27 it will say date correct!

But if it asks me for month and I'll type 6, as June and I write in 31 as days it will print which would say Month 6 doesn't have day 31.

I want to keep it simple so I understand like basic language not too hard! I'm just a fresh starter!

Thanks for all help.

Upvotes: 3

Views: 2851

Answers (5)

Basil Bourque
Basil Bourque

Reputation: 339372

tl;dr

Month.of( 6 ).maxLength()

java.time

The modern approach uses the java.time classes that supplant the troublesome old Date/Calendar classes.

Use the Month enum.

Month month = Month.of( 6 ) ;  // Months numbered sanely, 1-12 for January-December.
if( dayOfMonthInput > month.maxLength() ) {
    … bad input
}

Of course for February the last day of month depends on leap year. So if you want to be precise you need to use YearMonth.

YearMonth ym = YearMonth.of( 2017 , 2 ) ;
if( dayOfMonthInput > ym.lengthOfMonth() ) {
    … bad input
}

About java.time

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: 0

fireshadow52
fireshadow52

Reputation: 6516

This program should get you going...

import java.util.*;

class Date {
    public static void main(String[] args) {
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        System.out.print("Enter a month number and a day number (separate using \';\':_");
        Scanner sc = new Scanner(System.in).useDelimiter(";");
        int month = sc.nextInt();
        int day = sc.nextInt();
        sc.close();
        System.out.println((day == days[month - 1]) ? "Date correct!" : "Date incorrect: Month " + month + " does not have day " + day);
    }
}

Upvotes: 1

mcfinnigan
mcfinnigan

Reputation: 11638

Leveraging the functionality of the Calendar class is a far better way to do this.

    int day = 31;
    int month = 6; // JULY

    Calendar calendar = Calendar.getInstance();
    if(month < calendar.getActualMinimum(Calendar.MONTH) 
            || month > calendar.getActualMaximum(Calendar.MONTH))
        System.out.println("Error - invalid month");
    else 
    {
        calendar.set(Calendar.MONTH, month);
        if (day <  calendar.getActualMinimum(Calendar.DAY_OF_MONTH) 
                || day > calendar.getActualMaximum(Calendar.DAY_OF_MONTH))
        {
            System.out.println("Error - invalid day for month");
        }
        else
        {
            System.out.println("Date is correct");
        }
    }
}

All you have to be aware of is that Calendar currently treats January as month 0.

Upvotes: 0

aioobe
aioobe

Reputation: 421090

If you just want to get the job done, I'd suggest you go have a look at the Calendar API or perhaps JodaTime.

If you're more interested in learning how to do it yourself, here's one suggestion for an approach:

Hard-code an array like

int[] daysInMonths = { 31, 27, 31, ... };

and then check using something along the following lines:

// Get month and day from user
Scanner s = new Scanner(System.in);
int month = s.nextInt();
int day = s.nextInt();

int monthIndex = month - 1;     // since array indices are 0-based

if (1 <= day && day <= daysInMonths[monthIndex])
    print ok
else
    print does not exist

Upvotes: 3

Yasin Okumuş
Yasin Okumuş

Reputation: 2388

in Date class, the first month January is "0", ZERO. So you think June and you write 6, but must be 5; as

January 0
February 1
March 2
April 3
May 4
June 5

Upvotes: 0

Related Questions