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.

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

Unknown 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

Unknown 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

jobnewsbd24.com said...

NTRCA Job Circular 2021 will be released by the Authority. Non Government Teacher Registration & Certificate (NTRCA) official page: ntrca.gov.bd Otherwise our website can easily find all details found.

Anonymous said...

Todos os novos e antigos série de televisão soapsand web a partir de famosos canais de televisão brasileiros assistir a qualquer hora em Quanto Mais Vida Melhor Completo online Novela exclusiva da Tv Globo.

thittea said...

have a peek at this website good quality replica bags useful link best replica bags online learn this here now replica louis vuitton