5

I have the following date value 1995-12-31T23:59:59

but in order to parse this for a solr query I need it in the below format

1995-12-31T23:59:59Z

How can I parse this to get the added "Z" on the end in java 1.6 ?

The type must be java.util.date after the conversion - fyi

When I toString the date now and attempt to parse it with the SimpleDateFormat object it looks like this

"Mon Jan 01 00:00:00 CST 2001" - what is this format to convert it?

Dave Hill
  • 81
  • 1
  • 2
  • 6
  • 2
    Keep in mind that the 'Z' character indicates it is UTC time, so you might want to perform necessary zone conversions – dfb Jun 21 '11 at 17:14

3 Answers3

8

Use SimpleDateFormat:

  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
  Date d = df.parse("1995-12-31T23:59:59Z");
  System.out.println(d);

Put the 'Z' in single quotes to escape

nsfyn55
  • 14,875
  • 8
  • 50
  • 77
  • According to this [answer](http://stackoverflow.com/questions/833102/wheres-the-datetime-z-format-specifier/833143#833143), Z should has its own meaning. So I'm not sure this is what OP want. – Rangi Lin Jun 21 '11 at 17:13
0

Avoid Old Date-Time Classes

You are using the old java.util.Date/.Calendar and SimpleDateFormat classes. Avoid them.

The Date class has the poor design choice of its toString applying a default time zone when generating a String. So it seems like it has a time zone when in fact it does not (except one buried underneath that is ignored for regular use). Confusing, yes. Avoid it.

java.time

Instead use java.time built into Java 8 and later.

First parse as a LocalDateTime without any time zone or offset.

LocalDateTime ldt = LocalDateTime.parse( "1995-12-31T23:59:59Z" );

Apply a time zone or offset-from-UTC to give this LocalDateTime meaning, to make it an actual moment on the timeline. You have to know, or ask, what time zone or offset was intended by this string as no indication was embedded. For this example, I will arbitrarily assume Québec.

ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ldt.atZone( zoneId );

Your desired output has a Z on the end, for Zulu which means UTC.

In java.time an Instant represents a moment on the timeline in UTC. You can extract an Instant from the ZonedDateTime.

Instant instant = zdt.toInstant();

The Instant class’ toString method generates a string in your desired format. That format is one of the standard ISO 8601 formats.

String output = instant.toString();

Half-Open

I happened to notice that your example value was trying to get the end of 1995. There is a better way to do such search or comparison criteria.

In date-time work, the best practice is called Half-Open where the beginning of a span of time is inclusive while the ending is exclusive. So a week starts on Monday and runs up to, but not including, the next Monday.

Defining a year means starting at the first moment of the first day of 1995 and running up to but not including the first moment of the first day of the following year, 1996. Searching for any values within that range is done not with a BETWEEN but as: ( someEvent >= firstMomentOf1995 AND someEvent < firstMomentOf1996 ) ( not <= ).

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

"Z" is the time zone abbreviation for Zulu time zone i.e. UTC. If solr API accepts the date object, then you can just parse the date in the following way by setting preferred timezone:

SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
dateParser.setTimeZone(TimeZone.getTimeZone("Z"));
Date date = df.parse("1995-12-31T23:59:59");

If you need to convert it back to string then use the method provided by nsfyn55:

SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
System.out.println(dateFormatter.format()); 
Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132
  • My assumption is that he has the String "1995-12-31T23:59:59Z" most likely retrieved from from a xs:date timestamp and needs to convert it to a Date object for the Solr API. http://www.w3schools.com/schema/schema_dtypes_date.asp – nsfyn55 Jun 21 '11 at 20:34