Wednesday, June 02, 2004

Dates in Java.

Calendar class

Can anyone tell me why the Calendar class and the SimpleDateFormat class have so little to do with each other. For this uber-date selector widget that I'm producing I am using a date formatting string, based on SimpleDateFormat's, to tell the control what and how to display, I am also using a Calendar object to deal with the date arithmetic, i.e. rolling from 31st of December to 1st January in the next year.

I've had to build my own date format parser, because there is now way I can find to extract useful information about the date format string from SimpleDateFormat and absolutely no way to get useful textual descriptions of dates out of the Calendar without formatting a complete date.

What is worse is things like the Calendar returning int 0 for the value of January when getting the value of hte month field. I can understand that this is an index of the date, but this is not how any of the other fields work, and quite frankly my view on the matter is that 1 should be value of the month field for January.

Well I've gotten around most of these inconsistencies, for a start my date selector only deals with numerical representations of dates for now. I've created a calendar adaptor which sorts out the month offset problem and also now has get and set field value by string methods, which nicely handles the Era problem, BC=0 and AD=1.

I know that some of these issues are specific to the type of problem that I'm dealing with, but I am sure some of you must also have come across these. I had a scan through the Java bug database for Calendar bugs, but there were far too many to go through looking for these ones.

Dates are a big problem, one of the most recurring problems I have to deal with, but the Java core should be better than this. I won't tell you what we did when storing dates in the database, that's just evil!! ;)

11 comments:

Anonymous said...

simpleDateFormat.format( calendar.getTime() );

calendar.setTime( simpleDateFormat.parse(dateString) );

Matt Large said...

That's fine for dates as a whole, but I've been trying to work with individual fields within a date. It would be useful, for example, to be able to set Calendar fields with Strings that conform to a SimpleDateFormat, and be able to get String values back out.

At an even more simple level it would be useful to be able to convert from a date format block (i.e. "yyyy") to a Calendar field constant.

The current situation forces you to use the date as a whole, and when people are editing dates they are often doing this on only one part of it.

Anyway, I still don't get the January==0 December==11 thing.

Anonymous said...

an interesting read about this subject: http://oss.software.ibm.com/icu/docs/papers/international_calendars_in_java.html
quote: "Because of the problems I discussed above, IBM and Sun decided that Date was so broken that it couldn't be fixed and decided to replace it instead. But since Date was trying to do so many different things -- date formatting, calendar calculations, and time zones, we decided to replace it with several different classes."

Anonymous said...

interesting JDateTime class:
http://jodd.sourceforge.net/doc/time.html

Matt Large said...

I like the JDateTime class, solves many problems. Would still like to be able to access the parsed format template and use that as an index onto the date values. Still, a very good fix.

Junda Xu said...

20150709 junda
michael kors bags
true religion jeans
prada shoes
michael kors watches
ed hardy uk
tory burch shoes
coach factory outlet
chanel online shop
oakley store
oakley outlet
fitflops shoes
kate spade handbags
longchamp pliage
ralph lauren sale
kate spade
beats headphones
oakley sunglasses sale
kate spade outlet
nike blazer pas cher
chaussure louboutin pas cher
cheap ray bans
replica watches rolex
true religion sale
michael kors outlet
ray ban wayfarer
mcm backpack
coach outlet store online
abercrombie and fitch
burberry outlet
gucci uk
louboutin
michael kors outlet
michael kors handbags
mont blanc pens
cheap oakleys
michael kors outlet online
soccer shoes
ralph lauren homme
nike air force 1
gucci handbags

mao qiuyun said...

juicy couture outlet
soccer jerseys
ralph lauren outlet
hermes bags, http://www.hermesbags.co.uk/
christian louboutin outlet
louis vuitton bags, http://www.louisvuittonbag.us.com/
louis vuitton handbags
cheap nfl jerseys, http://www.cheap-nfljersey.us.com/
fitflop shoes
ugg boots, http://www.uggbootsclearance.in.net
oakley sunglasses wholesale, http://www.oakleysunglasseswholesale.in.net
michael kors uk
michael kors outlet
the north face clearance
manchester united jersey
iphone 6 cases
pandora
michael kors uk
rolex watches
coach outlet store, http://www.coachoutletstoreonline.in.net/
giuseppe zanotti outlet
converse all star
lululemon
air max 2015
mcm handbags
michael kors factory outlet, http://www.michaelkorsfactoryoutlets.in.net/
michael kors factory outlet, http://www.michaelkorsfactoryoutletonline.com
cheap soccer jerseys
true religion jeans, http://www.truereligionoutletstore.us.com
abercrombie
tory burch shoes
hermes birkin
coach outlet
tory burch outlet online
oakley sunglasses
0910maoqiuyun

风骚达哥 said...

20150930 junda
Abercrombie & Fitch Factory Outlet
Abercrombie T-Shirts
canada goose outlet online
coach factory outlet online
Wholesale Authentic Designer Handbags
Oakley Vault Outlet Store Online
Louis Vuitton Handbags Discount Off
nike trainers
michael kors outlet
coach factory outlet
cheap toms shoes
Michael Kors Outlet Online Mall
Coach Factory Outlet Online Sale
michael kors uk
Abercrombie And Fitch Kids Online
louis vuitton
true religion jeans
cheap jordan shoes
Louis Vuitton Handbags Official Site
Mont Blanc Legend And Mountain Pen Discount
cheap uggs
tory burch outlet
ugg boots
coach outlet
Michael Kors Handbags Huge Off
Hollister uk
michael kors handbags
louis vuitton outlet
uggs australia
Discount Ray Ban Polarized Sunglasses

chenlili said...

chenlili20160305
ray bans
adidas originals
jordan 11s
michael kors
air max 90
the north face
louis vuitton
mihchael kors outlet online
ray ban sunglasses outlet
adidas superstar
ugg boots
tods shoes
true religion
air jordan retro
celine
tory burch outlet
ugg outlet
cheap nfl jerseys
louis vuitton
the north face jackets
ralph lauren outlet
abercrombie
kate spade
louis vuitton handbags
coach outlet
michael kors outlet
michael kors outlet
lebron james shoes 12
abercrombie
ed hardy clothing
kate spade outlet
coach outlet store online
coach outlet online
hollister outlet
ugg outlet
louis vuitton

dong dong23 said...

mont blanc
tory burch outlet
michael kors handbags
louis vuitton handbags
nike roshe runs
michael kors outlet
louis vuitton outlet
christian louboutin sale
nike uk
true religion outlet
fitflops sale clearance
michael kors
coach outlet
hollister clothing store
adidas originals
nike basketball shoes
michael kors handbags
michael kors outlet clearance
coach outlet store online
michael kors bags
louis vuitton outlet
jordan 3 infrared
jordans
nike air max
true religion jeans
air jordan 13
louis vuitton outlet
michael kors outlet
louis vuitton outlet
ugg boots
coach outlet
true religion shorts
celine
kate spade
christian louboutin shoes
cheap oakleys
jordan 6
jordan shoes
louis vuitton outlet
oakley vault
20164.22wengdongdong

dada xu said...

michael kors outlet
lebron james shoes 2017
michael kors outlet
nike free run black
christian louboutin sale
kate spade handbags
michael kors
boston red sox jerseys
ralph lauren outlet online
coach factory outlet
chenshanshan20170318