I'm working with an ontology in Protegé 4.1 and I have a DataProperty which is a DateTime.
I'm getting this DateTime this way:
["2015-06-30T16:38:53"^^xsd:dateTime]
How do I put the date this way in a Java Date object? (I'm using Java 7)
I'm working with an ontology in Protegé 4.1 and I have a DataProperty which is a DateTime.
I'm getting this DateTime this way:
["2015-06-30T16:38:53"^^xsd:dateTime]
How do I put the date this way in a Java Date object? (I'm using Java 7)
If the string is even in the form it presented, also containing the XML data type, ie ["2015-06-30T16:38:53"^^xsd:dateTime]
, one way to do it would be to retrieve only the relevant part with an expression regular, and then use a DateFormat
.
An example regular expression would look like this:
\d+\-\d+\-\d+T\d+\:\d+\:\d+
A complete example would look like this:
final String dateFormat = "yyyy-MM-dd'T'HH:mm:ss";
final String dateOnOWL = "[\"2015-06-30T16:38:53\"^^xsd:dateTime]";
final Pattern pattern = Pattern.compile("\d+\-\d+\-\d+T\d+\:\d+\:\d+");
final Matcher matcher = pattern.matcher(dateOnOWL);
if (matcher.find()) {
final String result = matcher.group();
final DateFormat sdf = new SimpleDateFormat(dateFormat);
final Date date = sdf.parse(result);
System.out.println(date);
} else {
System.out.println(String.format("Padrão não encontrado em %s", dateOnOWL));
}
Regular expression is just an example of how to apply, you can improve it if you use this approach.
The simple way is to tell from which position the formatter ( DateFormat
) should start using the pattern we are using. To do this you should use a ParsePosition
informing that we will start of the 2 index position, that is 2 of 2015 . As the default will only be up to seconds, the formatter will ignore the rest.
An example would be something like this, which generates the same result:
final String dateFormat = "yyyy-MM-dd'T'HH:mm:ss";
final String dateOnOWL = "[\"2015-06-30T16:38:53\"^^xsd:dateTime]";
final DateFormat sdf = new SimpleDateFormat(dateFormat);
final ParsePosition position = new ParsePosition(2);
final Date date = sdf.parse(dateOnOWL, position);
System.out.println(date);