Perelum
Perelum

Reputation: 129

Java Web Service - better choice for date - java.util.Date limitations?

I'm building a web service in java, deploying it under Jboss-as-7.1.1.Final, based on org.apache.cxf.

I need to expose a web method that takes in input a date field that can reach many centuries in the past. At the moment I'm using java.util.Date. So, my web method is

public UpdatePersonResponse updatePerson(UpdatePersonRequest request) {
...
}

the request bean UpdatePersonRequest has:

...
private Date birthday;
...

In the soap request that I'm testing I put:

<!--Optional:-->
<birthday>1452-04-15</birthday>

The apache cxf logs show:

16:07:36,568 INFO  [org.apache.cxf.interceptor.LoggingInInterceptor] ... Inbound Message
[...]
<birthday>1452-04-15</birthday>

(this is correct)

A custom log I added into the set method of the request bean shows:

16:07:36,843 DEBUG  com.xxx.xxx.service.request.UpdatePersonRequest ... called setBirthday with argument [06/04/52 0.00]

(this is wrong)

Then the request bean is valorized as follows:

UpdatePersonRequest [... birthday=Thu Apr 06 00:00:00 CET 1452, ...]

(this is wrong)

So I obtain 1452 April 06, and not 1452 April 15.

I tried with different dates, for example, soap:

<birthday>1452-04-30</birthday>

custom log:

21:11:51,607 DEBUG  [com.xxx.xxx.service.request.UpdatePersonRequest]  ... called setBirthday with argument [21/04/52 0.00]

(wrong)

The bean is:

UpdatePersonRequest [... birthday=Fri Apr 21 00:00:00 CET 1452 ...]

(again wrong)

I obtain April 21 and not April 30.

I tried with several dates and noticed that the issue occurs for dates prior then 15 Oct 1582. For more recent dates all works fine and the request bean is correctly valorized.

Is there some java.util.Date limitation that I'm not considering? Should I use another java Type to represent these kind of dates, and what?

Thank you very much.

EDIT

The effect is that the date I entered into the soap request is converted in Julian date and then the Date I persist on the database will be this converted date. But I'd like to write on DB the exact date I enter in the request and not a corrected one, in other words I want the caller to deal with the right date system to use, and not any conversion be made ​​automatically by the system.

How can I obtain this?

Upvotes: 0

Views: 668

Answers (1)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79808

You are encountering the switch from the Julian to the Gregorian calendar. This is a historical event, not a feature of Java. Googling "Gregorian calendar" will tell you more.

Upvotes: 1

Related Questions