• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calendar class: Wrong values

 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone:

I am writing a program that will write to a file with the current date as of part of its filename.

However, I have run into a snag when using Calendar. Here is my code so far:



The IOException info will be put in once I am past this.

For example, since today is September 4, then the filename should be written as C://Users/sdl36/Desktop/PD_9-4-2012, which will give me just PD_9-4-2012 as the filename. However, when I used the println() method to test it, instead it gives me the filename PD_2_5_1.

Obviously it isn't the second month of the year, it's the 4th and not the 5th and it's not 2011 years into the past.

Can anyone tell or direct me as to what I am doing wrong? Or how the code should be changed?

Thanks everyone!

S.T.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code:
is not doing what you think it is doing.

Those are just constant values (magic numbers if you will) for identifying date fields.

Look at the javadoc for java.util.Calendar carefully.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. And yes, the Calendar class is one of the worst-designed classes in the Java ecosystem.
 
Sam Thompson
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked through all the methods or values that NetBeans showed in the menus for Calendar, and nothing looked helpful or useful.

Do you or anyone else have any alternatives to how I can do this? This shouldn't be difficult to pull off, I don't think. After all, you are just asking for three numbers.

S.T.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Letting an IDE think for you is going to lead you nowhere. Did you look at the javadoc for java.util.Calendar?
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Come on Bear, its your favourite class in the entire Java API, admit it!
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good grief, it's awful. Aside from just being poorly organized, the use of "magic numbers" is enough to relegate it to the 5th order of Hades.

But, if Sam can navigate its javadoc to find what he needs, he should be good to go for any other class that will be less, er, heinous.
 
Sam Thompson
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was a good point Bear...

At the same time I am looking through the Javadocs right now and I don't see anything that can give me directly what I need. All I see are methods and objects that will only give you field value information. Is there another class I can go to to just get the general date?

S.T.

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Thompson wrote:At the same time I am looking through the Javadocs right now and I don't see anything that can give me directly what I need. All I see are methods and objects that will only give you field value information.



Is that not exactly what you are asking for? There are three fields of the date you want the values to. Why would you not use the method that does exactly that?
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sam

You need to create a Calendar instance first before you can get out the date.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another hint. You want the get the value of, let's say, the year field. Or the month field. What's the first method name you'd think would do this for you?
 
Sam Thompson
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear:

I think I found a solution to the problem. Here is the code I've written so far:



However, the month it gives me is 8, and not 9- since we are now in September. Is there a way to fix it with another method? Or should I have to add one to the month value every time this program is used?

SAM
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Thompson wrote:However, the month it gives me is 8, and not 9- since we are now in September.


8 is correct. January is 0. Check out the value of Calendar.SEPTEMBER.

Is there a way to fix it with another method?


There's no need to "fix it", the value is correct.

Or should I have to add one to the month value every time this program is used?


Does that make sense? Or does that strike you as a band-aid?

You should only be using the month value to identify the month, You should not be using it to format a date for display. How should you format a date fro display?
 
Sam Thompson
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear:

I get what you are saying. It's just like with how arrays and lists are indexed right? The very first index is always ZERO. (DUH! Hand smacks head!) I should've known.

In this case though I am going to have to add one to it because I want it to show the correct date in the filename whenever I use the generated or save data for later analysis.

But I definetly see what you are getting at now.

Thank you so much for your help. It is greatly appreciated.

S.T.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Thompson wrote:In this case though I am going to have to add one to it because I want it to show the correct date in the filename whenever I use the generated or save data for later analysis.


Band-aid alert! Bad idea.

You didn't answer my question: what's the correct way to do tho?
 
Sam Thompson
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Haha...

*Scratching head*... I don't know. I always thought you had to add 1 to it. What other way (or WAYS) is/are there?
I just want the filename to say PD_9-4-2012.

Not September 4, 2012.

SAM
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cough, java.text.SimpleDateFormat, cough...
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I get what you are saying. It's just like with how arrays and lists are indexed right? The very first index is always ZERO. (DUH! Hand smacks head!) I should've known.



Actually, you shouldn't have. The fact that January is 0 and December is 11 is not a particularly nice feature of the Calendar class.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, they should be enums, but that's historical...
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*cough* Joda Time
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I was wondering when we should bring that up!
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must say the new date API for Java 8 looks promising:

http://sourceforge.net/apps/mediawiki/threeten/index.php?title=ThreeTen
 
Sam Thompson
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear...

Thanks for your help. I'll try what you suggested. I will let you know if I encounter any further problems with the Date situation.

I am going to keep this Forum open and unresolved until I know for sure that I have the date thing completely figured out.

SAM
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Boswell wrote:

I get what you are saying. It's just like with how arrays and lists are indexed right? The very first index is always ZERO. (DUH! Hand smacks head!) I should've known.



Actually, you shouldn't have. The fact that January is 0 and December is 11 is not a particularly nice feature of the Calendar class.


Especially since Sunday is 1 and Saturday is 7. There is no 0 there. That's why I often subtract Calendar.JANUARY or Calendar.SUNDAY to make my indexes 0-based. I know that subtracting Calendar.JANUARY does nothing but it makes it clear to the reader that I definitely want a 0-based index.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob

One of many workarounds I'm sure that developers over the years have had to come up with regards to Calendar!

When I was first introduced to Joda Time, I couldn't believe the ease of its use in comparison. Looking forward to Java 8 for the same reason.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Thompson wrote: . . .
. . .

Did you really? I obviously didn’t look at the thread properly on Tuesday, otherwise I would have noticed the three really bad bits of style in that code.
I challenge you to identify therm, and the 4th change I would make to that code. When you have done that, I shall suggest a 5th change.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic