• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date format using Caendar  RSS feed

 
Dave Jones
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

Here is a simple testing code I tried:

The printout was:
Month = 2
This is obviously wrong since we are in October which should be 10 not 2.

I anyone can shed some light this way, I'll be grateful
Dave
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are misinterpreting what Calendar.MONTH is. It is a class constant that is used to indicate what field you want to retrieve from a Calendar object.

Try using get(Calendar.MONTH), and it will retrieve the month associated with the Calendar object.
 
Dave Jones
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,

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)



Yours,
Bu.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young posted October 30, 2006 01:01 AM
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.


Hi ranchers,
I guess they were Maya(n).

Have a look on the Long Count,
Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!