You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by xor exor <ma...@gmail.com> on 2010/02/04 15:28:13 UTC

NodeType.setProperty behaviour

Hi,

In my application i'm trying to save my models' date fields as Gmt0 to my
database (my server is gmt+8)
so here is how i'm doing it :

Using jackrabbit 1.54

Node storyNode = newsNode.addNode(storyNodeName_,
ContentConstants.NODE_TYPE_STORY);
storyNode.setProperty("startDate",
ISO8601.format(ContentDateUtil.getCalToGMT0(story.getStartDate(),
ContentDateUtil.getUserTimeZoneOffset())),PropertyType.DATE);


and here is the  getCalToGMT0 :

public static GregorianCalendar getCalToGMT0(Date userDate,int
userTimeZoneOffset){
        //create a dummy calendar only for getting datefields
        Calendar tmpCal = Calendar.getInstance();
        tmpCal.setTime(userDate);

        //create a joda datimezone with customtimezoneoffset
        DateTimeZone jodaCustomTimeZone =
DateTimeZone.forOffsetHours(userTimeZoneOffset);
        //create a joda datetime with current userdate and
userTimeZoneOffset
        DateTime jodaCustomDateTime = new
DateTime(tmpCal.get(Calendar.YEAR),
                tmpCal.get(Calendar.MONTH)+1,
                tmpCal.get(Calendar.DAY_OF_MONTH),
                tmpCal.get(Calendar.HOUR_OF_DAY) ,
                tmpCal.get(Calendar.MINUTE),
                tmpCal.get(Calendar.SECOND),
                tmpCal.get(Calendar.MILLISECOND),
                jodaCustomTimeZone);

        //create a joda gmt time zone
        DateTimeZone gmtTimeZone = DateTimeZone.forOffsetHours(0);

        //convert custom timezone to gmt0
        DateTime dtGMT = jodaCustomDateTime.withZone(gmtTimeZone);
        GregorianCalendar gmtGreg = dtGMT.toGregorianCalendar();

        return gmtGreg;
    }


Here is the summary of my code

i create a Node then put a calendar which is set to GMT0,but somehow the
setNodeProperty is messing with date and saving it as wrong value to the
database
btw the value that comes from : ISO8601.format is OK ,but setProperty is
doing something nasty ? Any suggestions how to solve problem ? Is it a bug
or feature ?

Re: NodeType.setProperty behaviour

Posted by Thomas Müller <th...@day.com>.
Sorry... was meant for Stefan only...

Thomas

Re: NodeType.setProperty behaviour

Posted by Thomas Müller <th...@day.com>.
Hallo Stefan

> what database?

Hat er doch gesagt: gmt+8: "my database (my server is gmt+8)"

>> Using jackrabbit 1.54

Aha. Ein Zukunftsmensch.

;-)
Thomas

Re: NodeType.setProperty behaviour

Posted by Alexander Klimetschek <ak...@day.com>.
I just noted that I missed that you actually posted your code in the
first mail, and you also use the joda DateTime API. Then my comments
were a bit off ;-) See below for some more notes.

On Thu, Feb 4, 2010 at 15:53, xor exor <ma...@gmail.com> wrote:
> On Thu, Feb 4, 2010 at 6:08 PM, Thomas Müller <th...@day.com>wrote:
>> c.setTimeZone(TimeZone.getTimeZone("UTC"));
>
> Tried that also it doesnt work :|. I created a calendar with gmt0 set attrs
> and then created a fresh one as you sugested and set all fields and finally
> set the timezone. That way when i enter a date 18:00 GMT+2 i got in db 19:00
> GMT+3 how funny is that :)

There is no "UTC" timezone ID in java, you should try "Etc/UTC"

Also Calendar.getInstance() is very dangerous, as it uses the default
timezone of the current JVM (mostly not what you want)

Try this:

public Calendar getUTCDate(Calendar in) {
    Calendar out = Calendar.getInstance(TimeZone.getTimeZone("Etc/UTC"));
    out.setTimeInMillis(in.getTimeInMillis());
    return out;
}

then use it like this (assuming the story.getStartDate() already has
the proper user timezone):

storyNode.setProperty("startDate", getUTCDate(story.getStartDate()));

(there is a setProperty() method that accepts a Calendar as value and
saves it right as jcr DATE property).


>> But the Calendar methods are a bit dangerous, see also
>> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4827490

Yes, one has to know which combinations are safe ;-) The order of
calling methods is sometimes important, as not always the internal
time will be recalculated...

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: NodeType.setProperty behaviour

Posted by xor exor <ma...@gmail.com>.
On Thu, Feb 4, 2010 at 6:08 PM, Thomas Müller <th...@day.com>wrote:

> Hi,
>
> I think what happens is that in your getCalToGMT0 method you both
> change the timezone to UTC (which is probably what you want) but also
> the time.
>
> I would try using:
>
> Calendar c = Calendar.getInstance();
> ... set fields accordingly, if needed ...
> c.setTimeZone(TimeZone.getTimeZone("UTC"));
>

Tried that also it doesnt work :|. I created a calendar with gmt0 set attrs
and then created a fresh one as you sugested and set all fields and finally
set the timezone. That way when i enter a date 18:00 GMT+2 i got in db 19:00
GMT+3 how funny is that :)


>
> But the Calendar methods are a bit dangerous, see also
> http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4827490
>
> Yes Calendar and all its relatives suck in Java. Btw,thanks for your help!


> Regards,
> Thomas
>

Re: NodeType.setProperty behaviour

Posted by Thomas Müller <th...@day.com>.
Hi,

I think what happens is that in your getCalToGMT0 method you both
change the timezone to UTC (which is probably what you want) but also
the time.

I would try using:

Calendar c = Calendar.getInstance();
... set fields accordingly, if needed ...
c.setTimeZone(TimeZone.getTimeZone("UTC"));

But the Calendar methods are a bit dangerous, see also
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4827490

Regards,
Thomas

Re: NodeType.setProperty behaviour

Posted by xor exor <ma...@gmail.com>.
On Thu, Feb 4, 2010 at 5:29 PM, Alexander Klimetschek <ak...@day.com>wrote:

> On Thu, Feb 4, 2010 at 14:15, xor exor <ma...@gmail.com> wrote:
> > On Thu, Feb 4, 2010 at 5:06 PM, Alexander Klimetschek <aklimets@day.com
> >wrote:
> >
> >> On Thu, Feb 4, 2010 at 13:57, xor exor <ma...@gmail.com> wrote:
> >> > Well for example i'm getting the date as 2010 12 1 17:00 GMT+2 the
> time
> >> that
> >> > i get from
> >> > ISO8601.format is OK i mean it is GMT0 15:00 then i try to save that
> >> value
> >> > to db with setProperty the date value is messed to something like
> GMT+4
> >> > 19:00
> >>
> >> Should be obvious: 15:00 GMT+00:00 == 19:00 GMT+04:00
> >>
> >
> > Of course it is but what i want to do is save time in database as  15:00
> > GMT+00:00 not as
> > 19:00 GMT+04:00 that is what my method is doing (which source is in my
> first
> > post). My method is creating a Calendar with GMT0 as timezone and passing
> it
> > to setProperty method ,but somehow my date is messed and saved
> differently
> > (with different timezone)to database that is the problem. I hope that is
> > clearer.
>
> Look at your Calendar object (ie. log all important fields, including
> the timezone) directly before you hand it over to setProperty(). I
> guess it does not look like you want. The Java date API is not
> necessarily intuitive and has all its quirks. Just replacing the
> timezone with GMT+0 does not necessarily work, in most cases it is
> best to start with a fresh Calendar object in the new timezone and
> pass it the utc millis from the old calendar.
>

Here is my Calendar that is what i got from eclipse debug

java.util.GregorianCalendar[time=1292256000111,
areFieldsSet=true,
areAllFieldsSet=true,
lenient=true,
zone=java.util.SimpleTimeZone[id=UTC,
offset=0,
dstSavings=3600000,
useDaylight=false,
startYear=0,
startMode=0,
startMonth=0,
startDay=0,
startDayOfWeek=0,
startTime=0,
startTimeMode=0,
endMode=0,
endMonth=0,
endDay=0,
endDayOfWeek=0,
endTime=0,
endTimeMode=0],
firstDayOfWeek=1,
minimalDaysInFirstWeek=1,
ERA=1,
YEAR=2010,
MONTH=11,
WEEK_OF_YEAR=51,
WEEK_OF_MONTH=3,
DAY_OF_MONTH=13,
DAY_OF_YEAR=347,
DAY_OF_WEEK=2,
DAY_OF_WEEK_IN_MONTH=2,
AM_PM=1,
HOUR=4,
HOUR_OF_DAY=16,
MINUTE=0,
SECOND=0,
MILLISECOND=111,
ZONE_OFFSET=0,
DST_OFFSET=0]

Which means 13 December 2010 16:00 GMT:00

What i have saved in my database is :

13 December 2010 18:00 GMT+2



>
> The setProperty(Date) method should never change anything, it
> internally will only serialize the date as a iso8601 string and then
> stores it like a string property internally.
>

I see,but that is what i got,i spent lots of time digging that Calendar
objects,i hope someone can help me .


>
> Regards,
> Alex
>
> --
> Alexander Klimetschek
> alexander.klimetschek@day.com
>

Re: NodeType.setProperty behaviour

Posted by Alexander Klimetschek <ak...@day.com>.
On Thu, Feb 4, 2010 at 14:15, xor exor <ma...@gmail.com> wrote:
> On Thu, Feb 4, 2010 at 5:06 PM, Alexander Klimetschek <ak...@day.com>wrote:
>
>> On Thu, Feb 4, 2010 at 13:57, xor exor <ma...@gmail.com> wrote:
>> > Well for example i'm getting the date as 2010 12 1 17:00 GMT+2 the time
>> that
>> > i get from
>> > ISO8601.format is OK i mean it is GMT0 15:00 then i try to save that
>> value
>> > to db with setProperty the date value is messed to something like GMT+4
>> > 19:00
>>
>> Should be obvious: 15:00 GMT+00:00 == 19:00 GMT+04:00
>>
>
> Of course it is but what i want to do is save time in database as  15:00
> GMT+00:00 not as
> 19:00 GMT+04:00 that is what my method is doing (which source is in my first
> post). My method is creating a Calendar with GMT0 as timezone and passing it
> to setProperty method ,but somehow my date is messed and saved differently
> (with different timezone)to database that is the problem. I hope that is
> clearer.

Look at your Calendar object (ie. log all important fields, including
the timezone) directly before you hand it over to setProperty(). I
guess it does not look like you want. The Java date API is not
necessarily intuitive and has all its quirks. Just replacing the
timezone with GMT+0 does not necessarily work, in most cases it is
best to start with a fresh Calendar object in the new timezone and
pass it the utc millis from the old calendar.

The setProperty(Date) method should never change anything, it
internally will only serialize the date as a iso8601 string and then
stores it like a string property internally.

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: NodeType.setProperty behaviour

Posted by xor exor <ma...@gmail.com>.
On Thu, Feb 4, 2010 at 5:06 PM, Alexander Klimetschek <ak...@day.com>wrote:

> On Thu, Feb 4, 2010 at 13:57, xor exor <ma...@gmail.com> wrote:
> > Well for example i'm getting the date as 2010 12 1 17:00 GMT+2 the time
> that
> > i get from
> > ISO8601.format is OK i mean it is GMT0 15:00 then i try to save that
> value
> > to db with setProperty the date value is messed to something like GMT+4
> > 19:00
>
> Should be obvious: 15:00 GMT+00:00 == 19:00 GMT+04:00
>

Of course it is but what i want to do is save time in database as  15:00
GMT+00:00 not as
19:00 GMT+04:00 that is what my method is doing (which source is in my first
post). My method is creating a Calendar with GMT0 as timezone and passing it
to setProperty method ,but somehow my date is messed and saved differently
(with different timezone)to database that is the problem. I hope that is
clearer.


>
> The java.util.Calendar object contains the timezone (to be exact: the
> timezone difference, ie. +4 in this case) and this is stored in JCR as
> well. So normally just taking a Calendar object in the local timezone
> and giving it to JCR works well.
>

Yeah i know what i want is to save a calendar with GMT0 to database
correctly not with local timezone.

>
> Everything else can be done by using operations on the Calendar object
> to convert from and to other timezones. Which is a topic on its own,
> that can be answered by Google ;-)
>
Thanks but i googled it a lot ;) I think the problem is how setProperty
handles the dates. Should look at source for more specific info.

>
> Regards,
> Alex
>
> --
> Alexander Klimetschek
> alexander.klimetschek@day.com
>

Re: NodeType.setProperty behaviour

Posted by Alexander Klimetschek <ak...@day.com>.
On Thu, Feb 4, 2010 at 13:57, xor exor <ma...@gmail.com> wrote:
> Well for example i'm getting the date as 2010 12 1 17:00 GMT+2 the time that
> i get from
> ISO8601.format is OK i mean it is GMT0 15:00 then i try to save that value
> to db with setProperty the date value is messed to something like GMT+4
> 19:00

Should be obvious: 15:00 GMT+00:00 == 19:00 GMT+04:00

The java.util.Calendar object contains the timezone (to be exact: the
timezone difference, ie. +4 in this case) and this is stored in JCR as
well. So normally just taking a Calendar object in the local timezone
and giving it to JCR works well.

Everything else can be done by using operations on the Calendar object
to convert from and to other timezones. Which is a topic on its own,
that can be answered by Google ;-)

Regards,
Alex

-- 
Alexander Klimetschek
alexander.klimetschek@day.com

Re: NodeType.setProperty behaviour

Posted by xor exor <ma...@gmail.com>.
On Thu, Feb 4, 2010 at 4:43 PM, Stefan Guggisberg <
stefan.guggisberg@gmail.com> wrote:

> On Thu, Feb 4, 2010 at 3:28 PM, xor exor <ma...@gmail.com> wrote:
> > Hi,
> >
> > In my application i'm trying to save my models' date fields as Gmt0 to my
> > database (my server is gmt+8)
> > so here is how i'm doing it :
> >
> > Using jackrabbit 1.54
> >
> > Node storyNode = newsNode.addNode(storyNodeName_,
> > ContentConstants.NODE_TYPE_STORY);
> > storyNode.setProperty("startDate",
> > ISO8601.format(ContentDateUtil.getCalToGMT0(story.getStartDate(),
> > ContentDateUtil.getUserTimeZoneOffset())),PropertyType.DATE);
> >
> >
> > and here is the  getCalToGMT0 :
> >
> > public static GregorianCalendar getCalToGMT0(Date userDate,int
> > userTimeZoneOffset){
> >        //create a dummy calendar only for getting datefields
> >        Calendar tmpCal = Calendar.getInstance();
> >        tmpCal.setTime(userDate);
> >
> >        //create a joda datimezone with customtimezoneoffset
> >        DateTimeZone jodaCustomTimeZone =
> > DateTimeZone.forOffsetHours(userTimeZoneOffset);
> >        //create a joda datetime with current userdate and
> > userTimeZoneOffset
> >        DateTime jodaCustomDateTime = new
> > DateTime(tmpCal.get(Calendar.YEAR),
> >                tmpCal.get(Calendar.MONTH)+1,
> >                tmpCal.get(Calendar.DAY_OF_MONTH),
> >                tmpCal.get(Calendar.HOUR_OF_DAY) ,
> >                tmpCal.get(Calendar.MINUTE),
> >                tmpCal.get(Calendar.SECOND),
> >                tmpCal.get(Calendar.MILLISECOND),
> >                jodaCustomTimeZone);
> >
> >        //create a joda gmt time zone
> >        DateTimeZone gmtTimeZone = DateTimeZone.forOffsetHours(0);
> >
> >        //convert custom timezone to gmt0
> >        DateTime dtGMT = jodaCustomDateTime.withZone(gmtTimeZone);
> >        GregorianCalendar gmtGreg = dtGMT.toGregorianCalendar();
> >
> >        return gmtGreg;
> >    }
> >
> >
> > Here is the summary of my code
> >
> > i create a Node then put a calendar which is set to GMT0,but somehow the
> > setNodeProperty is messing with date and saving it as wrong value to the
> > database
>
> setNodeProperty? what are you refering to? there's no such method in
> the JCR api.
>

Yes sorry it is org.apache.jackrabbit.core.NodeImpl.setProperty


>
> what wrong value? what database?
>

Well what i'm doing is getting user (in user's timezone) date convert it to
GMT0 and saving to repository.Which obviosly uses jcr,
btw the db is mysql

>
> > btw the value that comes from : ISO8601.format is OK ,but setProperty is
> > doing something nasty ?
>
> what is wrong?
>

Well for example i'm getting the date as 2010 12 1 17:00 GMT+2 the time that
i get from
ISO8601.format is OK i mean it is GMT0 15:00 then i try to save that value
to db with setProperty the date value is messed to something like GMT+4
19:00


>
> > Any suggestions how to solve problem ?
>
> what problem?
>

?


>
> cheers
> stefan
>
> > Is it a bug
> > or feature ?
> >
>

Re: NodeType.setProperty behaviour

Posted by Stefan Guggisberg <st...@gmail.com>.
On Thu, Feb 4, 2010 at 3:28 PM, xor exor <ma...@gmail.com> wrote:
> Hi,
>
> In my application i'm trying to save my models' date fields as Gmt0 to my
> database (my server is gmt+8)
> so here is how i'm doing it :
>
> Using jackrabbit 1.54
>
> Node storyNode = newsNode.addNode(storyNodeName_,
> ContentConstants.NODE_TYPE_STORY);
> storyNode.setProperty("startDate",
> ISO8601.format(ContentDateUtil.getCalToGMT0(story.getStartDate(),
> ContentDateUtil.getUserTimeZoneOffset())),PropertyType.DATE);
>
>
> and here is the  getCalToGMT0 :
>
> public static GregorianCalendar getCalToGMT0(Date userDate,int
> userTimeZoneOffset){
>        //create a dummy calendar only for getting datefields
>        Calendar tmpCal = Calendar.getInstance();
>        tmpCal.setTime(userDate);
>
>        //create a joda datimezone with customtimezoneoffset
>        DateTimeZone jodaCustomTimeZone =
> DateTimeZone.forOffsetHours(userTimeZoneOffset);
>        //create a joda datetime with current userdate and
> userTimeZoneOffset
>        DateTime jodaCustomDateTime = new
> DateTime(tmpCal.get(Calendar.YEAR),
>                tmpCal.get(Calendar.MONTH)+1,
>                tmpCal.get(Calendar.DAY_OF_MONTH),
>                tmpCal.get(Calendar.HOUR_OF_DAY) ,
>                tmpCal.get(Calendar.MINUTE),
>                tmpCal.get(Calendar.SECOND),
>                tmpCal.get(Calendar.MILLISECOND),
>                jodaCustomTimeZone);
>
>        //create a joda gmt time zone
>        DateTimeZone gmtTimeZone = DateTimeZone.forOffsetHours(0);
>
>        //convert custom timezone to gmt0
>        DateTime dtGMT = jodaCustomDateTime.withZone(gmtTimeZone);
>        GregorianCalendar gmtGreg = dtGMT.toGregorianCalendar();
>
>        return gmtGreg;
>    }
>
>
> Here is the summary of my code
>
> i create a Node then put a calendar which is set to GMT0,but somehow the
> setNodeProperty is messing with date and saving it as wrong value to the
> database

setNodeProperty? what are you refering to? there's no such method in
the JCR api.

what wrong value? what database?

> btw the value that comes from : ISO8601.format is OK ,but setProperty is
> doing something nasty ?

what is wrong?

> Any suggestions how to solve problem ?

what problem?

cheers
stefan

> Is it a bug
> or feature ?
>