File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes DateFormat problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "DateFormat problem" Watch "DateFormat problem" New topic
Author

DateFormat problem

Wayne Burr
Ranch Hand

Joined: Oct 15, 2002
Posts: 38
This Java code is inside a JSP so I hope I am posting in the correct forum:
I'm getting the following error when I try to format a String to a date:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
Here is where I am trying to format inside of the JSP:
-----
<%
SimpleDateFormat formatter = new java.text.SimpleDateFormat("DD-MMM-yyyy");

int factorIndex = -1;

factorLMEList = connector.getFactorLMEs();
session.setAttribute("factorLMEList", factorLMEList);
FactorLME nextfactor = new FactorLME();
String startdate = "";
String enddate = "";
String orgunit = "";
String fstartdate = "";
String fenddate = "";
%>
<%
for (int i=0;i<factorLMEList.getNumberOfFactorLME();i++) {
nextfactor = factorLMEList.getFactorLMEs(i);
startdate = formatter.format(((String)nextfactor.getstartDate()));
enddate = formatter.format(((String)nextfactor.getendDate()));
orgunit = nextfactor.getorgUnit();
if (i == factorIndex) {
%>
.
.
.
-----
any insight on how I can accomplish getting the date to be in the format of "01-JAN-2002", would be great. Thanks!
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Clarification question:
What does the method call below return- a plain java.util.Date object?
nextfactor.getstartDate()
Wayne Burr
Ranch Hand

Joined: Oct 15, 2002
Posts: 38
These are the two methods in FactorLME:
public String getstartDate()
{
return startDate;
}
public String getendDate()
{
return endDate;
}
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
DateFormat
format takes in a Date and converts it to a String.
parse takes in a String and converts it to a Date.
What the code is doing is passing a String to format when it requires a Date object. There are a few things you can do to correct this:
1) Change startDate & endDate in FactorLME to be java.util.Date objects instead of String objects. This also requires updating the two get methods to return Date instead of String. When you call the code below don't cast the return object of the gets to String
startdate = formatter.format(nextfactor.getstartDate());
enddate = formatter.format(nextfactor.getendDate());
2) Since startDate & endDate are already strings, then you can format them to the format that you want when you set those values.
Depending on what type you want startDate and endDate to be will determine how you want to go about getting the format that you want.
Any questions? I hope that made sense.
Wayne Burr
Ranch Hand

Joined: Oct 15, 2002
Posts: 38
Still having problems. I hope that I am missing something simple..
This is the current error that I am getting:
org.apache.jasper.JasperException: Unable to compile class for JSPNote: sun.tools.javac.Main has been deprecated.
C:\Tomcat\work\localhost_8080%2Fka\_0002fjsp_0002fFactors_0002ejspFactors_jsp_37.java:262: Incompatible type for =. Can't convert java.util.Date to java.lang.String.
startdate = df.parse(nextfactor.getstartDate());
---------
The startdate is a String but holds a date with time and all I want it to do is display the date
Additional help would be great...Thanks!
Wayne Burr
Ranch Hand

Joined: Oct 15, 2002
Posts: 38
Additional Code:
---
<%

int factorIndex = -1;
DateFormat df = DateFormat.getDateInstance();
factorLMEList = connector.getFactorLMEs();
session.setAttribute("factorLMEList", factorLMEList);
FactorLME nextfactor = new FactorLME();
String startdate = "";
String enddate = "";
String orgunit = "";
String fstartdate = "";
String fenddate = "";
%>
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
Let's break this down first:
(1) nextfactor.getstartDate() returns a String
(2) This String is passed to df.parse(...)
According to the javadoc, the parse method of java.text.DateFormat takes in a String and returns a java.util.Date object:
From http://java.sun.com/j2se/1.4.1/docs/api/java/text/DateFormat.html : public Date parse(String source)
(3) You assign the resulting Date object to startdate, which is a String object (bad!)
Result: Can't convert java.util.Date to java.lang.String.

The above jsp code is in essence what is happening when you assign the result from df.parse(...) to startdate.
Solution:
Since your startdate variable is a String, we'll fix this by making sure that the right hand side (RHS) of the assignment statement results in a String object, after all the evaluation.
Thankfully the java.util.Date class has a method which converts itself into a String object : String toString() (http://java.sun.com/j2se/1.3/docs/api/java/util/Date.html)

In your code, it might make more sense if I break down your original line

[ December 05, 2002: Message edited by: David Duran ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DateFormat problem