Jena以xml dateTime存储日期 - 意外行为

时间:2014-07-27 06:54:55

标签: java rdf jena ontology

我使用…,model.createTypedLiteral(cal))来存储xml dateTime dataTypeProperty的值。

问题是,时间没有保存在我当地时区(UTC + 05:00)伊斯兰堡,卡拉奇)PKT。当我System.get.out.println(cal)时,它显示我在当地时区的时间。但是当我执行此操作…,model.createTypedLiteral(cal))并将其写在owl文件上时,小时部分将被保存,减去5小时(-05:00)。这是我的代码:

...
1)Date date = new Date();
2)Calendar cal = Calendar.getInstance();
3)cal.setTime(date);

4)System.out.println(cal);
...
5)Resource resource = model.createResource(resourceURI);
6)resource.addProperty(model.createProperty(baseURI+propertyName), model.createTypedLiteral(cal));
...
write model on owl file
...

第4行打印

java.util.GregorianCalendar[time=1406441262261,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Karachi",offset=18000000,dstSavings=0,useDaylight=false,transitions=12,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=31,WEEK_OF_MONTH=5,DAY_OF_MONTH=27,DAY_OF_YEAR=208,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=11,HOUR_OF_DAY=11,MINUTE=7,SECOND=42,MILLISECOND=261,ZONE_OFFSET=18000000,DST_OFFSET=0]

请参阅HOUR=11, HOUR_OF_DAY=11正确的当地时间。

但是第6行和第6行继续将相同的内容写入owl文件,并将其保存在owl中

<created rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime"
>2014-07-27T06:07:42.261Z</created>

这是我当地时间的-5:00。

我已编写代码以从owl文件中检索并在屏幕上打印

    1)List<String> dlist = new ArrayList<String>();
    2)dlist= ...;
    3)System.out.println(dlist.get(0));

     String s = dlist.get(0);
     SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
     try {
        Date date1 = sd.parse(s);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
    4)  System.out.println(cal);
    5)  System.out.println(cal.getTime()+" "+cal.getTimeZone());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

输出:

第3行

2014-07-27T06:07:42.261Z^^http://www.w3.org/2001/XMLSchema#dateTime

第4行

java.util.GregorianCalendar[time=1406423262000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Karachi",offset=18000000,dstSavings=0,useDaylight=false,transitions=12,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2014,MONTH=6,WEEK_OF_YEAR=31,WEEK_OF_MONTH=5,DAY_OF_MONTH=27,DAY_OF_YEAR=208,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=7,SECOND=42,MILLISECOND=0,ZONE_OFFSET=18000000,DST_OFFSET=0]

第5行

Sun Jul 27 06:07:42 PKT 2014 sun.util.calendar.ZoneInfo[id="Asia/Karachi",offset=18000000,dstSavings=0,useDaylight=false,transitions=12,lastRule=null]

所有输出显示-5:00

的时间

请指导我问题在哪里?

=====================

PS

如果上面的例子很复杂,那么这里很简单:

    Model model = ModelFactory.createDefaultModel();
    Resource resource = model.createResource("http://example.com/resource/test");
    Property property = model.createProperty("http://example.com/prop/test");
    Calendar cal = GregorianCalendar.getInstance();
    System.out.println("Current time is. cal="+cal.getTime());
    Literal value = model.createTypedLiteral(cal);
    System.out.println("Current time saved in value as= "+value);
    String s1 = value.toString();
    SimpleDateFormat sd1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date3;
    try {
        date3 = sd1.parse(s1);
        cal.setTime(date3);
        System.out.println("Retrieving back the time from value variable as date="+cal.getTime());
    resource.addProperty(property, value);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

输出:

Current time is. cal=Sun Jul 27 13:04:53 PKT 2014
Current time saved in value as= 2014-07-27T08:04:53.681Z^^http://www.w3.org/2001/XMLSchema#dateTime
Retrieving back the time from value variable as date=Sun Jul 27 08:04:53 PKT 2014

在结果中,第一行显示的时间与第二行不同。为什么会这样?

1 个答案:

答案 0 :(得分:2)

尝试

中的代码
com.hp.hpl.jena.sparql.util.NodeFactoryExtra.dateTimeToNode

或它使用的例程:

com.hp.hpl.jena.sparql.util.Utils.calendarToXSDDateTimeString(...)

获取词法形式,然后创建类型为xsd; dateTime的类型文字。

这些补偿了Xerces标准化为时区Z的事实。