• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

DateFormat problem

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clarification question:
What does the method call below return- a plain java.util.Date object?
nextfactor.getstartDate()
 
Wayne Burr
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are the two methods in FactorLME:
public String getstartDate()
{
return startDate;
}
public String getendDate()
{
return endDate;
}
 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic