• Post Reply Bookmark Topic Watch Topic
  • New Topic

Day of year with users input.  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm doing a problem where I convert users input for the date (MM/DD/YY) into a written out date and year. That part is fine. The part I'm struggling with is then indicating what day that is in that year.
Can someone please explain what I'm doing wrong? I'm not sure how to use the user inputted MONTH, DAY, and YEAR to then get the day of year.

(The part I'm struggling with is at the bottom of the code.)

Thanks.

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why reinventing the wheel?

You expect your date to be passed in specific format, right? So use DateFormat class. Your format is simple so SimpleDateFormat will suffice.
You don't want it to be lenient. No things like February 30th allowed. So setLenient to false.
Then, you can easily parse the String giving you a Date object.
What can you do with such object? Well, you might create a Calendar instance and setTime to that date.
Then, you can get the DAY_OF_YEAR. Problem solved!

It is that simple. I've written this using 6 lines of code.

And what is the main error in your program?
It's this lineIt is an equivalent of
And this means set the calendar's year to 1 BC (year 0 does not exist), month to February (check the value of Calendar.FEBRUARY) and day of month to 2?
Also, Calendar is a decent class that takes values in logical order: year, month, day. From biggest to lowest unit.
Not like silly american way of writing month, day, year ;) ;) ;)

And your code for retrieving a date is wrong. For most cases it works. But try typing 02/29/12 which is legal date.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would it be something like this? (I don't know what to put as the stringDate but figured I'd at least ask.)

I tried the date you gave and see it doesn't work but I'm too fried at this point to realize what to do.

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:Would it be something like this? (I don't know what to put as the stringDate but figured I'd at least ask.)

I tried the date you gave and see it doesn't work but I'm too fried at this point to realize what to do.

In your stringDate there should be your user input that you want to process.
Check very carefully the documentation of SimpleDateFormat. What does mm mean?
If you want to parse a year using two digits don't use yyyy. If you use yyyy then the input 14 would be treated as the year 14. Use yy instead so 14 is parsed as 2014.
You are using - as a separator in date format. I thought you wanted to use /

If you read my previous post again you'll see that I basically told you exactly the code to write ;) (maybe except what to use as a date format - check that in javadoc).
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:I tried the date you gave and see it doesn't work but (...)

What didn't work? Remember It Doesnt Work Is Useless.

EDIT:

Ah, I get it now. You meant 29th of February date doesn't work. How could it? You specified in your code that February can have max 28 days.
I am just allergic to phrase it doesn't work so don't worry about it ;).
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help Pawel. I'm away from my pesonal computer but will try again tonight.

And yeah, I meant about the date not working. Thanks for pointing that out actually.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the best I can come up with and it's wrong. I feel like I'm close but I don't understand how I get what the user inputted at the JOptionPane to be used
to calculate the day since if I use "userInput" anywhere it gives me an error. This is the first time I've worked with the calendar.

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:to calculate the day since if I use "userInput" anywhere it gives me an error.

What error exactly?
I posted exact steps to follow in my firt post in this topic. What should I explain better?
I wrote:1. You expect your date to be passed in specific format, right? So use DateFormat class. Your format is simple so SimpleDateFormat will suffice.
2. You don't want it to be lenient. No things like February 30th allowed. So setLenient to false.
3. Then, you can easily parse the String giving you a Date object.
4. What can you do with such object? Well, you might create a Calendar instance
5. and setTime to that date.
6. Then, you can get the DAY_OF_YEAR. Problem solved!


Here, I numbered things you have to do. Each number is one line of code. Number 2 is optional but I advise you to use it.

A hint. On this list, whenever I posted something you need to use (class name, method, constant) I used this font. Hope this will get easier now ;).

And the String I mentioned in point 3 is your user input.
So add point 0. to the list:

0. Get date string from the user.

Well, you actually did this already in line 45 of your code


But be careful. You said you want month, day, year format viz "Please enter the date in MM/DD/YY format"
and then you expect a date in format: year, month, day viz SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd");

You need to decide which one you want to use.

Did I confuse you with telling that Calendar uses year, month, day? I was referring to an order of arguments in one of Calendar's methods.
You can use any order you want in your SimpleDateFormar. Even a silly one as month, day, year ;) ;)
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I honestly don't know how to do (2) but will try to figure it out later. I tried to go line by line with your hints and this is what I got. I keep getting a "Unhandled exception typeParseException"
error. My understanding of that is I'm formatting the date incorrectly but I am not seeing how.

Sorry if I'm being annoying. I get obsessed with this stuff.

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2. would be format.setLenient(false).

Ryan Bishop wrote:My understanding of that is I'm formatting the date incorrectly but I am not seeing how.

That's incorrect. It is compile time error related to not handling a checked exception.

And about this exception. You know how to handle exceptions right?
If not, read this.
You have two options.
1. Add throws clause to the method.
2. Put the code that might throw an exception in try block.

You wroteSo, are you getting day of year here? Check carefully.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think I got it? (Added a throws ParseException at the main method.)

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!