Results 1 to 20 of 26
Thread: Another Date Problem
- 08-14-2011, 05:27 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
Another Date Problem
The following code:
keeps returning:Java Code:String dateString = request.getParameter("date"+Integer.toString(j)); Date myDate = new Date(dateString);
(For the new Date line)Java Code:java.lang.IllegalArgumentException java.util.Date.parse(Date.java:615) java.util.Date.<init>(Date.java:272)
The request is definitely returning a String, some other error code even told me so. Can't figure out what's wrong, help appreciated =(
(j is just an index in a loop btw and the naming system works fine on other parameters)
- 08-14-2011, 05:50 PM #2
What is in dateString? Print it out before using it in the method call.
- 08-14-2011, 05:51 PM #3
so what happens when you use
do you get a simple string in standard out?Java Code:System.out.println(request.getParameter("date"+Integer.toString(j)));
Not only that but using Date like that is deprecated. As of JDK version 1.1, Date(String s) has been replaced by DateFormat.parse(String s). Try using DateFormat.parse(String s) and see what happens.My API:Java Code:cat > a.out || cat > main.class
- 08-14-2011, 06:06 PM #4
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
I'm using this code within JSP so system.out doesn't seem to work. However if I set a string to the getParameter thing, and then print it out using jsp i get a list of dates like "Mon Dec 27 00:00:00 GMT 2010"
Can you explain how to use DateFormat.parse()? I'm getting all sorts of errors and I've even looked at the api.
I'm trying to do this at the moment...
Date myDate = DateFormat.parse(dateString);
Brings up the error:
Cannot make a static reference to the non-static method parse(String) from the type DateFormat
-
- 08-14-2011, 06:48 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
Sorry for being a beginner =\
How should I use a SimpleDateFormat instance to do my parsing? I've used it before but I'm not sure how you're suggesting I use it in this case. Am I still supposed to use DateFormat.parse()?
- 08-14-2011, 07:00 PM #7
Here, this should work in order to get your String into a date.
You need to create a DateFormat object in order to parse a date for you.Java Code:import java.text.DateFormat; import java.util.Date; public class dating { public static void main(String[] cheese) { String myNewDate = "Jun 12, 1337"; DateFormat df = DateFormat.getDateInstance(); Date myDate = new Date(); try { myDate = df.parse(myNewDate); } catch (java.text.ParseException e) { } System.out.println(myDate); } }My API:Java Code:cat > a.out || cat > main.class
- 08-14-2011, 07:32 PM #8
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
Thankyou! That was very helpful. Except now it keeps saying "Unparsable date "[date]"", no matter what format i put it in. I even just hardcoded a date in like in your example, but still no joy =\
-
Please read the SimpleDateFormat API and it will show you all about how to use format Strings with this.
- 08-14-2011, 08:01 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
I did... and it didn't.
-
- 08-14-2011, 08:43 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
Oki doke
Code:
Error:Java Code:String myNewDate = "Jun 12, 1337"; DateFormat df = DateFormat.getDateInstance(); Date myDate = new Date(); myDate = df.parse(myNewDate);
Java Code:org.apache.jasper.JasperException: An exception occurred processing JSP page /enterFormClassAttendanceSuccess.jsp at line 37 34: <% String myNewDate = "Jun 12, 1337"; 35: DateFormat df = DateFormat.getDateInstance(); 36: Date myDate = new Date(); 37: myDate = df.parse(myNewDate); 38: String student = request.getParameter("presentStudent"+Integer.toString(j)); %> 39: 40: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:519) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause javax.servlet.ServletException: java.text.ParseException: Unparseable date: "Jun 12, 1337" org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:865) org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:794) org.apache.jsp.enterFormClassAttendanceSuccess_jsp._jspService(enterFormClassAttendanceSuccess_jsp.java:200) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause java.text.ParseException: Unparseable date: "Jun 12, 1337" java.text.DateFormat.parse(DateFormat.java:354) org.apache.jsp.enterFormClassAttendanceSuccess_jsp._jspService(enterFormClassAttendanceSuccess_jsp.java:159) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
-
Again, I would recommend that you use a SimpleDateFormat object, not a DateFormat format object, and again the SimpleDateFormat API will show you how to use it. I don't see your attempt at this yet.
Also, I don't code JavaServerPages, but do you want your Java code inside the JSP?
-
For example:
Java Code:import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateTest { public static void main(String[] args) { String formatString = "MM/dd/yyyy"; // the SimpleDateFormat API will help you here SimpleDateFormat sdf = new SimpleDateFormat(formatString); String dateString = "6/12/1337"; try { Date date = sdf.parse(dateString); System.out.println(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
- 08-14-2011, 09:21 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 08-14-2011, 10:14 PM #16
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
O.o
Sorry Fubarable, I thought you were talking about DateFormat for some reason. Thanks for that, realised I'd actually used SimpleDateFormat in another page.
I read through the API anyway, and I came up with this:
This is the code that passes the parameter:
This is the code that gets the parameter:Java Code:<%SimpleDateFormat sdfB = new SimpleDateFormat("yyyy-MM-dd"); String passDate = sdfB.format(theDate);%> <input type=hidden name="<%=dateString%>" value="<%=passDate%>">
(At this point if i print date0 it gives me a date in the format yyyy-MM-dd so I know the formatting in the first page is working)Java Code:SimpleDateFormat sdfC = new SimpleDateFormat("yyyy-MM-dd"); String dateString = request.getParameter("date"+Integer.toString(j)); Date myDate = sdfC.parse(dateString);
EDIT: Another point, I just looked over that code and was thinking 'why don't I just pass the date object instead of converting it to a string?'. It's because request.getParameter("date"+Integer.toString(j)) returns a String, whether the variable is a String or a Date, apparently.
And, as always, this is the error I'm getting now.
Java Code:org.apache.jasper.JasperException: An exception occurred processing JSP page /enterFormClassAttendanceSuccess.jsp at line 38 35: 36: SimpleDateFormat sdfC = new SimpleDateFormat("yyyy-MM-dd"); 37: String dateString = request.getParameter("date"+Integer.toString(j)); 38: Date myDate = sdfC.parse(dateString); 39: 40: String student = request.getParameter("presentStudent"+Integer.toString(j)); %> 41: Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:519) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:428) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) root cause java.lang.NullPointerException java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1232) java.text.DateFormat.parse(DateFormat.java:352) org.apache.jsp.enterFormClassAttendanceSuccess_jsp._jspService(enterFormClassAttendanceSuccess_jsp.java:163) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:717) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)Last edited by UnfairBear; 08-15-2011 at 01:05 AM.
- 08-15-2011, 11:10 AM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
dateString is null at that point.
Can I ask why you are doing this in a JSP?
This is the sort of thing that should be learnt via servlets. JSPs are something to learn after you've figured out how Java works on a server.
- 08-15-2011, 02:18 PM #18
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
If I print dateString after I initialise it, it prints a date, so it's not null the whole time. I don't know why it says it's null at that point. Also If i print it, or if I just print something like "date3" it prints in the form "Mon Dec 27 00:00:00 GMT 2010" instead of "2010-12-27". I can't figure out why it's not formatting in the first page...
EDIT: I changed the SimpleDateFormat object to keep the "EEE MMM dd HH:mm:ss z yyyy" format, so the formatting shouldn't be a problem... I hope. It's just the null pointer I'm confused about at this stage, it doesn't seem to make sense.
I'm doing it in JSP because I learned it a couple of years ago in college (before we even learned Java) and it was the best thing that I have in my knowledge to use for the project I'm currently doing. I didn't have time to learn a whole new language or much else, so I'm working with what I have. Right now I have until the end of this month to finish the project, and this Date thing is something that's really vital to completing it properly, so I really do appreciate all the help; I've gotten a lot further than I would have on my own. I'm sorry if my noobishness is frustrating =\Last edited by UnfairBear; 08-15-2011 at 02:26 PM.
- 08-15-2011, 02:24 PM #19
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
That is giving you null for dateString, implying that you have no parameter "date<n>". (Note that you could simply do "date"+j).Java Code:String dateString = request.getParameter("date"+Integer.toString(j));
Why, I can't say.
As for the date format problem, how are you printing the date? If you're not using a SimpleDateFormat to format it then it will simply use the default format.
- 08-15-2011, 06:11 PM #20
Member
- Join Date
- Jul 2011
- Posts
- 29
- Rep Power
- 0
Yeah, I see that. But those parameters do exist. I can't figure out why the parameter is there when printing but not there when parsing.
I printed dateString by using
<c:out value="<%=dateString%>"/>
.. and a single parameter by using
<c:out value="${param.date3}"/>
(The parameters have dynamic names which is why I cant just use date1 date2 date3 etc, the names have to be initialised on the fly)
Those ways both print the correct values.
"(At this point if i print date0 it gives me a date in the format yyyy-MM-dd so I know the formatting in the first page is working)"
"Also If i print it, or if I just print something like "date3" it prints in the form "Mon Dec 27 00:00:00 GMT 2010" instead of "2010-12-27". I can't figure out why it's not formatting in the first page..."
Yay for contradictions! Apparently the formatting in the first page stopped working and I have no idea why. The code seems to be exactly the same. *SIGH*.
EDIT: Hm.. I discovered something interesting.
This code:
Brings up the "java.lang.NullPointerException" from earlier, on the line with the stars.Java Code:SimpleDateFormat sdfC = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy"); **Date myDate = sdfC.parse(request.getParameter("date"+Integer.toString(j)));
However this code:
Brings up the error "javax.servlet.ServletException: java.text.ParseException: Unparseable date: "Mon Dec 27 00:00:00 GMT 2010"" on the same line. Note the only difference is the SimpleDateFormat constructorJava Code:SimpleDateFormat sdfC = new SimpleDateFormat("dd/MM/yyyy"); Date myDate = sdfC.parse(request.getParameter("date"+Integer.toString(j)));
The parameter coming in is in the "EEE MMM dd HH:mm:ss z yyyy" format, which I assume is why it's not parsable in the second bit of code, but that means that the parameter is being recognised by the function in the second bit of code, but not in the first. This.. doesn't make any sense! DDDD:Last edited by UnfairBear; 08-15-2011 at 06:22 PM.
Similar Threads
-
Add Date Problem
By UnfairBear in forum New To JavaReplies: 2Last Post: 08-14-2011, 02:10 PM -
Add Date Problem
By UnfairBear in forum Java AppletsReplies: 0Last Post: 08-14-2011, 10:56 AM -
Add Date Problem
By UnfairBear in forum Java ServletReplies: 0Last Post: 08-13-2011, 09:33 PM -
sql date problem
By realosso in forum New To JavaReplies: 3Last Post: 06-04-2010, 04:42 AM -
date problem
By karlkwanny in forum JDBCReplies: 1Last Post: 07-30-2009, 10:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks