last day (15 days later) » 

4:10 PM
0
A: Date change when converting from XMLGregorianCalendar to Calendar

Pavel HoralExcerpt from XMLGregorianCalendar.toGregorianCalendar() JavaDoc on how they create the GregorianCalendar instance: Obtain a pure Gregorian Calendar by invoking GregorianCalendar.setGregorianChange( new Date(Long.MIN_VALUE)). This means, that the created calendar will be proleptic and won't...

 
Thanks Pavel. I unfortunately can't use Joda in this situation, can you show bullets 2 and 3 please?
 
I've updated the answer with some background information on Date and Calendar interactions. Also I have deleted the second bullet as it was not correct advice and would lead you into more problems.
 
That last part just made it more confusing. How would I apply your solution to what I have? In other words where would I incorporate ((GregorianCalendar) calendar).setGregorianChange(new Date(Long.MIN_VALUE)); in my setXMLGregorianCalendar method and use it to get the date from the GregorianCalendar variable?
 
You should call this method every time you create or obtain new Calendar instance.
Also the point I was trying to make was that the date was never incorrect... it was just displayed in Julian system. Conversion to proleptic Gregorian can be made just before you display it to user if needed. But there is nothing wrong if you force every calendar in your application to be proleptic. However Date#toString will probably always switch to Julian and display "incorrect" value.
 
I have to send a type of Date so I can't convert it back to Gregorian, the methods shown in my code are the ones I have to use. So, can you either 1) Show how to force the calendars to be proleptic or 2) Show how to properly "call this method every time you create or obtain new Calendar instance" as what you provided does not resolve my issue when used in the example I provided.
 
4:11 PM
> I have to send a type of Date so I can't convert
Then you don't have to do anything. Dates are not connected to calendar systems. It would be the responsibility of your caller to use correct calendar system.
 
hey Pavel
thanks for making the chat
I don't have a choice here in terms of calendar system, it's a bug in the backend layer I'm forced to use
I would like to detail in my report of it how the bug is causing the issue our users are seeing, and provide some sort of solution. Using the code you provided, I reran the example in my question, but it is still being affected by the Julian change
 
Just for this discussion you should distinguish between the physical representation of instant and its human-readable representation...
The java.util.Date is a timestamp - physical representation of an instant.
There is no bug there...
The bug you are probably referring to is when you try to display it to your users.
 
The bug I'm referring to is the setTime(getTime()) where the calendar is being switched to Julian. I just need it to remain Gregorian
when it's changed there, that new value is propagated down the line to where in the example it is changed to Date type, and sent off
 
But you have said that you return java.util.Date, correct?
 
I don't, but the backend does. I'm testing that code in question. But yes, that final tyhpe is Date
 
4:22 PM
java.util.Date is always independent of calendar system... you can not bind java.util.Date to Gregorian system. That was my point.
The real bug is in in the place where someone interprets the timestamp using Julian calendar.
But presents it as gregorian date.
 
Exactly, which is where I would like to apply the solution, before it's sent off down the line
 
Let me try to "draw" an information flow chain...
 
What you have provided in your answer I have tried, but it is remaining as Julian
The only solution I have had work for the result I want, is instead of using getTime(), using the field accessors of Calendar.get(), but if there's an easier way to collect all of the information from the Calendar variable coming in, I'd prefer it done that way
 
XML (gregorian human-readable representation) => reading it into Java (XMLGregorianCalendar) => converting to Calendar (proleptic GregorianCalendar) => getting java.util.Date (timestamp, completely independent of calendar system) => "sending it down the line" (now we are out of your code) => someone tries to display the date using GregorianCalendar which switches to Julian system for the timestamp
The thing is that when you get java.util.Date, it does not have any relation to calendar system... it is up to the code, which displays this date to user to use the correct calendar.
 
No, that's not what's happening. It's XML (gregorian human-readable representation) => reading it into Java (XMLGregorianCalendar) => converting to Calendar (proleptic GregorianCalendar) => Becomes Julian type => getting java.util.Date FROM calendar.getTimeinMillis(), which becomes wrong on the consumer end
we are not "displaying" the date to a user, it is being take and stored on a database
 
4:30 PM
What database are you using?
 
the form directly before it is put there, would look like "0001-01-01"
that's not really important, but I don't have access to it either way
 
My point is that the bug is in JDBC drivers ;)
 
yea, it's not been fun. This code was written in 2006, and part of the lib we use
I was hoping there was some other way to create my Calendar type that when Date = new Date(calendar.getTimeinMillis()) was called, the millis would be Gregorian and not Julian
instead of having to do field accessors
 
Please just confirm -> you understand that java.util.Date is timestamp (number of milliseconds since UNIX epoch) and not connected to calendar system
(just want the confirmation to ask a different question)
 
yes
I get the "value" is and has been correct the whole time, but the representation is wrong
 
4:34 PM
Also do you agree that 0000-01-03 (Julian) == 0000-01-01 (Gregorian)?
 
yes
100% with you
 
Then 0000-01-01 (Julian) != 0000-01-01 (Gregorian)
 
correct
 
then the only outcome of Date.toString for (0000-01-01 Gregorian) is being "0000-01-03"
 
no, that is not correct.
like I said, I have a solution using the field accessors which will give me 0001-01-01
 
4:36 PM
But that approach is then making 0000-01-01 (Julian) != 0000-01-01 (Gregorian) ... be careful... this is why i have deleted "the second bullet" in my answer
Because default Calendar will switch to Julian for this date.
But I understand that this might "solve" your problem... even thou it uses incorrect date.
 
so you're saying then, that I'm not sending the original value, but simply replacing the new values month/year/day fields with the old?
it's a bit of a hack, which is why I wanted a different solution if possible
 
The only solution for you would be to turn of the "magical" switch in GregorianCalendar.
 
I understand the solution I currently have is giving a "julian" representation of what the given value "looked like" in gregorian
 
Not sure this is possible.
> I understand the solution I currently have is giving a "julian" representation of what the given value "looked like" in gregorian
exactly
it is a different instant
if you would do a calendar arithmetic on it, you would get wrong answers
(e.g. number of days between that date and the current date)
 
But would I ever get "wrong" end values compared to the input date?
i.e. a date that didn't "look" like the former
 
4:40 PM
depends on the code... in correct code you should get a different date.... also if you use something like JAXB which strictly uses proleptic Gregorian calendar, you might get wrong date
but if you only want to make the broken legacy code work, you might be sage
*safe
 
It's pretty simple going from Date->XMLGregorianCalendar, if I had to overhaul their method of doing the opposite, is there an easier/better way to do so than by using any Calendar/GregorianCalender types at all?
 
Not that I am aware of...
 
ok, thanks for the help with this, and for creating a chat
 
You are welcome... hope you'll be able to solve your problem.
 
5:08 PM
// Get XML gregorian calendar
XMLGregorianCalendar xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar();
xmlCalendar.setYear(1); // Watch for octal number representations (you had there 0001)
xmlCalendar.setMonth(1);
xmlCalendar.setDay(1);

// Convert to Calendar as it is easier to work with it
Calendar calendar = xmlCalendar.toGregorianCalendar(); // Proleptic for old dates

// Convert to default calendar (will misinterpret proleptic for Julian, but it is a workaround)
Calendar result = Calendar.getInstance();
This is what you probably wanted to see in the beginning. But I just wanted to make sure you understand that you get incorrect instant by this approach.
If you want to implement something incorrect, you better know what you are doing :D.
 

last day (15 days later) »