| Author |
Transforming Dates with SimpleDateFormat
|
Robert Popular
Greenhorn
Joined: Oct 14, 2002
Posts: 6
|
|
All: I am getting the current date/time by performing java.util.Date date = (Calendar.getInstance()).getTime() It returns this format: Wed Oct 16 11:41:29 CDT 2002 I need to transform this into the following format: yyyyMMddHHmmssZ Using the SimpleDateFormat as follows, I get an Unparseable date error. public String getLDAPDateFormat(java.util.Date date) { SimpleDateFormat sdfDateFormat = new SimpleDateFormat(BatchConstants.DATE_LDAP_PATTERN); try { date = sdfDateFormat.parse(date.toString()); System.out.println("REMOVE): LastLogin=" +date.toString()); } catch(java.text.ParseException pe) { pe.printStackTrace(); } return date.toString(); } Can you tell me where the gap in my thinking is? How can I get the first format into the second? Thanks in advance! bp
|
 |
Robert Popular
Greenhorn
Joined: Oct 14, 2002
Posts: 6
|
|
Ok, So I found the hole in the thinking and replaced the code with the following: However, I am getting the following error with the 'Z' in my pattern yyyyMMddHHmmssZ. Any ideas? Thanks!
|
 |
James Swan
Ranch Hand
Joined: Jun 26, 2001
Posts: 403
|
|
If you wanted the timezone info to appear on your formatted date then you should use lowercase 'z' eg. yyyyMMddHHmmssz Or if you wanted the literal 'Z' to appear at the end of the formatted date the do this: ldapDate = sdfDateFormat.format(date) + "Z";
|
 |
Dave Landers
Ranch Hand
Joined: Jul 24, 2002
Posts: 401
|
|
|
Z is a new formatting character for JDK 1.4 to handle RFC 822 time zones. If you are looking at the 1.4 docs, make sure you are running with a 1.4 JDK.
|
 |
 |
|
|
subject: Transforming Dates with SimpleDateFormat
|
|
|