日期转换前一天

时间:2019-08-28 09:57:14

标签: java xsd calendar jena

假设我有这个字符串:

String date = "18-7-1495"

我想在Apache Jena中将其定义为xsd:dateTime

所以我做了以下事情:

DateFormat df = new SimpleDateFormat ("dd-MM-yy");
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse(date));
x.addProperty(DCTerms.date, model.createTypedLiteral(new XSDDateTime(cal));

问题在于此日期现在存储为:

dcterms:date            "1495-07-17T23:00:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> ;

为什么日期17不是18?

2 个答案:

答案 0 :(得分:0)

DateFormat df =新的SimpleDateFormat(“ dd-mm-yy”)

此处mm被认为是分钟,因此要格式化月份,您需要使用MM。这样可以解决问题

答案 1 :(得分:0)

当您将时间添加到模型new XSDDateTime(cal)时,会发生问题。

问题

String SOURCE = "http://www.w3.org/2002/07/owl#";
Model model = ModelFactory.createDefaultModel();

DateFormat df = new SimpleDateFormat("dd-MM-yy");
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse("18-7-1495"));

Resource testResource = model.createResource(SOURCE + "test");
testResource.addProperty(DCTerms.date, model.createTypedLiteral(new XSDDateTime(cal)));

model.write(System.out);

输出:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://purl.org/dc/terms/" > 
  <rdf:Description rdf:about="http://www.w3.org/2002/07/owl#test">
    <j.0:date rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">1495-07-17T23:00:00Z</j.0:date>
  </rdf:Description>
</rdf:RDF>

解决方案

String SOURCE = "http://www.w3.org/2002/07/owl#";
Model model = ModelFactory.createDefaultModel();

DateFormat df = new SimpleDateFormat("dd-MM-yy");
Calendar cal = Calendar.getInstance();
cal.setTime(df.parse("18-7-1495"));

Resource testResource = model.createResource(SOURCE + "test");
testResource.addProperty(DCTerms.date, model.createTypedLiteral(cal.getTime()));

model.write(System.out);

输出:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:j.0="http://purl.org/dc/terms/" > 
  <rdf:Description rdf:about="http://www.w3.org/2002/07/owl#test">
    <j.0:date rdf:datatype="java:java.util.Date">Sat Jul 18 00:00:00 CET 1495</j.0:date>
  </rdf:Description>
</rdf:RDF>