File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Transforming Dates with SimpleDateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Transforming Dates with SimpleDateFormat" Watch "Transforming Dates with SimpleDateFormat" New topic

Transforming Dates with SimpleDateFormat

Robert Popular

Joined: Oct 14, 2002
Posts: 6
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:
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);
date = sdfDateFormat.parse(date.toString());
System.out.println("REMOVE): LastLogin=" +date.toString());
catch(java.text.ParseException pe)
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!
Robert Popular

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?
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'
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.
I agree. Here's the link:
subject: Transforming Dates with SimpleDateFormat
It's not a secret anymore!