Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Date problem

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Date problem

    Hi everyone,

    I am trying to create a program that asks the user for the date and then checks if this date is the last day of the month. However I am having problems doing this. Could anyone suggest where to start? I can get the user to input a day using scanner or JOptionPane but then im not sure where to go from there?

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Date problem

    Take a look at the Date, Calendar, and DateFormat classes. The API is your friend.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Date problem

    I've written a Date class, I could send you the code with controller with the logic, just have a muck about with that. But days of each month are (from Jan - Dec):

    31, 28*, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31

    Put that into a matrix and set up an interger for the day and month. So:

    IF month==10
    THEN LET numberOfDays=31

    Just compare your day integer to that. If it's not equal, then return false, else return true. Simple?

    Also note that if you're going to use an array or matrix, the computer counts each entry from zero, so to get the value from a matrix, you'll have to say something like:

    LET mon=noOfDays[month-1]
    IF day==mon
    THEN LET endOfMonth=TRUE
    ELSE LET endOfMonth=FALSE

    Or at least that's something close to pseudo-code. See the logic? If you do, that's half of the problem solved.

    Regards,

    Shaun.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Date problem

    No offense intended, but I think that's reinventing the wheel to a certain extent. That's fine if the purpose of the exercise is to learn (like using an array when an ArrayList is more appropriate), but this functionality already exists in the classes I mentioned. Up to you though.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Thumbs up Re: Date problem

    Quote Originally Posted by KevinWorkman View Post
    No offense intended, but I think that's reinventing the wheel to a certain extent. That's fine if the purpose of the exercise is to learn (like using an array when an ArrayList is more appropriate), but this functionality already exists in the classes I mentioned. Up to you though.
    The course I'm on requires you to build your own Date class because it presents an interesting challenge.

    I'm not great at remembering exact syntax, but isn't there a boolean condition for Date, something like dateVar.isEarlierThan(testDate), which works something like this:

    if (!dateVar.isEarlierThan(testDate)) { return false; }
    else return true;

    Not 100% sure on that one, in fact, I might be thinking of another language altogether.

    But, if I'm somewhere close to being correct, as long as your testDate is the last day of the month, it'll work.

    Regards,

    Shaun.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Date problem

    Calendar and Date both have before() and after() methods, as well as compareTo() methods.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Dec 2009
    Location
    UK
    Posts
    58
    My Mood
    Sleepy
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Date problem

    A note about ArrayList types: some earlier Java IDEs don't like this. I know because the College has a lot of really old software installed on their PCs. I don't understand why educational institutions have an aversion to upgrading free software?!? Oh well... Anyway, we run a really old version of NetBeans, and also a slightly newer version of BlueJ. I can't remember which way around it is, but one of them is happy with ArrayLists and the other isn't.

    The only reason I'm pointing this out is that some businesses here in the UK also run 'legacy' systems. When I did telesales in 2006ish, a warehouse was using an old 486-DX25 terminal to run and maintain its' database. I was like "it's like being at home: where is the new technology???!!!" I suppose some business thinking is 'why upgrade and pay for staff training etc... if the status quo works'.

    So, don't always assume that clients have the latest kit and laters JDKs or whatever.

    Regards,

    Shaun.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Date problem

    I'd bet it's more likely that the older systems don't "like" generics. They are used pretty often with ArrayLists, and have only been around since Java 1.5 (or somewhere around there, I'm too lazy to look it up right now), and ArrayLists have been around since 1.2.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. JAVASCRIPT DATE DIFFERENCE PROBLEM
    By jai in forum Other Programming Languages
    Replies: 3
    Last Post: April 5th, 2011, 09:11 AM
  2. Editing a date
    By waterjames in forum Java Theory & Questions
    Replies: 0
    Last Post: November 27th, 2010, 02:00 PM
  3. DATE.java!
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 3rd, 2010, 05:37 PM
  4. sql date problem
    By realosso in forum JDBC & Databases
    Replies: 2
    Last Post: June 3rd, 2010, 08:32 AM
  5. How to set expiry date for cms?
    By khodam in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 4th, 2009, 01:15 PM