Vik
Vik

Reputation: 9289

failed date conversion in java

I am seeing a string as Wed Apr 27 00:00:00 GMT-700 1988 and to convert it to date I did

Date dateOfBirth = new Date(bean.getUserProfileBean().getDateOfBirth());

This fails and I am not sure why. Any idea if it is specific to GAE?

Upvotes: 1

Views: 121

Answers (4)

Vik
Vik

Reputation: 9289

to parse -700 the required format was Z,ZZ

Upvotes: 0

Peter Knego
Peter Knego

Reputation: 80330

The date Wed Apr 27 00:00:00 GMT-700 1988 is not in a format that Java can parse out of the box. Specifically, the timezone GMT-700 part is not parsable by any library that I know of.

This format is not any of the standard timezone formats: general timezone, RFC822 or ISO8601.

You will need to write your own parser for that.

Upvotes: 1

CloudyMarble
CloudyMarble

Reputation: 37566

Try:

stirng strDate = bean.getUserProfileBean().getDateOfBirth();
Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy",
  Locale.ENGLISH).parse(strDate );

See this answer

Alternatively you can try to use the DateTimeFormatter of Joda-Time, although you may encounter problems with the 'z' for timezone names whih is conventional:

stirng strDate = bean.getUserProfileBean().getDateOfBirth();
DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE MMM d HH:mm:ss z yyyy");
dtf.parseDateTime(strDate);

Upvotes: 0

BobTheBuilder
BobTheBuilder

Reputation: 19284

Date has empty constructor or Date(long)

If you want to get date from String, you need to use SimpleDateFormat

Upvotes: 0

Related Questions