Hi all ranchers ! I have a small problem and I'm sure there is a simple answer out there... well here goes.. I need to parse the current date into a simple string like this: (lets take today for example) "061029" Now, I can't see how I can use Date class without using deprecated methods, so I thought of using Calendar. Problem is I don't see how I can "initialize" it and get true results. (when I try getInstance() and then MONTH and others I get wrong results)
Yes, I see. Thank you, I understand now how this works. now I can get exactly what I need. I see that the day(of month) is correct, though the month is "short by one" I wonder why they started counting the months from zero but the day-of-month from one. anyway, thank you!
basically you will need four classes. Calendar you got already. But you will the Date class also, as kind of a bridge. To format / parse the dates you will need the classes DateFormat and SimpleDateFormat. They are both not in java.util but in the java.text package. The API explains how to use Strings to format dates for weekdays months ... take this as an example:
prints: 061029 (but only today...) The letters used for SimpleDateFormat are case sensitive, try out yymmdd for fun.
DateFormat is for general date formats, SimpleDateFormat is for "home made". you can also use SimpleDateFormat to get your Month, see example "MM" -> 10 "MMM" -> Oct "MMMM"-> October - the whole month name (Locale dependent)
Originally posted by Dave Jones: I see that the day(of month) is correct, though the month is "short by one" I wonder why they started counting the months from zero but the day-of-month from one.
A lot of people wonder about this. In my opinion, it was a mistake by the people who wrote the first version of the Java API to count months from 0 to 11 instead of 1 to 12. The Java API isn't perfect and bugfree, you know... Unfortunately it is impossible to repair it in a newer version of Java, because it would break compatibility with older versions.