3

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.

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
Sebastian
  • 29
  • 1
  • 6
  • 1
    Sorry mate, I couldn't stand all those typos - edited lot of them. – adarshr Sep 08 '11 at 11:11
  • Haha! Thanks for fixing it, I have been figuering how to write this program for a while now, I have seem to loose my ability to type. – Sebastian Sep 08 '11 at 11:14
  • This is the code I have written I know its awful. public class Date { public static void main(String[] args) { System.out.println("write in month number :"); int month; month = 7; if (month <1 ) { if (month >12){ } System.out.println("month "" has no such day"); } } } – Sebastian Sep 08 '11 at 11:45
  • If doing real work rather than homework or practice, know that this behavior is largely implemented in the [`java.time.Month`](https://docs.oracle.com/javase/8/docs/api/java/time/Month.html) enum. – Basil Bourque Aug 30 '16 at 05:39

5 Answers5

3

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
aioobe
  • 413,195
  • 112
  • 811
  • 826
1

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);
    }
}
fireshadow52
  • 6,298
  • 2
  • 30
  • 46
  • I'd suggest changing from BufferedReader to Scanner. – aioobe Sep 08 '11 at 11:29
  • @aioobe OK, but is there a specific reason? – fireshadow52 Sep 08 '11 at 11:30
  • Shorter lines, better readability, 1 line instead of 9 (!) for reading an integer. – aioobe Sep 08 '11 at 11:33
  • Thank you all for your replies, Im ashamed that I still dont understand the basics. Im trying to run your code but I cant seem to get it to work running. Your class Date, is it something I should change since my class name is something diffrent and im getting erros on some lines, can you try running it? Thanks again for your time. – Sebastian Sep 08 '11 at 11:43
  • @Sebastian Which lines are causing errors? I ran it and didn't get any errors. – fireshadow52 Sep 09 '11 at 01:08
0

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

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
Yasin Okumuş
  • 2,299
  • 7
  • 31
  • 62
  • Me too. It is always confusing... And now we have Calendar, that makes everything more simpler and also complex and confusing :) – Yasin Okumuş Sep 08 '11 at 11:15
  • it is an important point to remember, but the user can still input `6` for June, it is the program that must convert it to the correct internal number. – user85421 Sep 08 '11 at 11:17
0

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.

mcfinnigan
  • 11,442
  • 35
  • 28