• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Date format using Caendar

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
They weren't very bright, but they were very, very big. Ad contrast:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic