Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default Another Date Problem

    The following code:
    Java Code:
    String dateString = request.getParameter("date"+Integer.toString(j));
    					Date myDate = new Date(dateString);
    keeps returning:
    Java Code:
    java.lang.IllegalArgumentException
    	java.util.Date.parse(Date.java:615)
    	java.util.Date.<init>(Date.java:272)
    (For the new Date line)

    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)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    What is in dateString? Print it out before using it in the method call.

  3. #3
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    so what happens when you use
    Java Code:
    System.out.println(request.getParameter("date"+Integer.toString(j)));
    do you get a simple string in standard out?

    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

  4. #4
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    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

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by UnfairBear View Post
    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
    This is basic Java: Don't call the method on the class. Instead create a SimpleDateFormat instance and use it to do your parsing.

  6. #6
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    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()?

  7. #7
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default

    Here, this should work in order to get your String into a date.

    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);
    	}
    }
    You need to create a DateFormat object in order to parse a date for you.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  8. #8
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    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 =\

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #10
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    I did... and it didn't.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by UnfairBear View Post
    I did... and it didn't.
    If you're still stuck, then show your code, show your full error message, and indicate which line of code causes the error (show the actual line of code, not just the number for best results).

  12. #12
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    Oki doke

    Code:
    Java Code:
    String myNewDate = "Jun 12, 1337";
    DateFormat df = DateFormat.getDateInstance();
    Date myDate = new Date();
    myDate = df.parse(myNewDate);
    Error:
    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)

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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();
          }
       }
    }

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by UnfairBear View Post
    I've even looked at the api.
    Somehow I find this behaviour fascinating.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    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:
    Java Code:
    <%SimpleDateFormat sdfB = new SimpleDateFormat("yyyy-MM-dd");
    String passDate = sdfB.format(theDate);%>
    <input type=hidden name="<%=dateString%>" value="<%=passDate%>">
    This is the code that gets the parameter:
    Java Code:
    SimpleDateFormat sdfC = new SimpleDateFormat("yyyy-MM-dd");
    String dateString = request.getParameter("date"+Integer.toString(j));
    Date myDate = sdfC.parse(dateString);
    (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)

    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.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    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.

  18. #18
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    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.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Java Code:
    String dateString = request.getParameter("date"+Integer.toString(j));
    That is giving you null for dateString, implying that you have no parameter "date<n>". (Note that you could simply do "date"+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.

  20. #20
    UnfairBear is offline Member
    Join Date
    Jul 2011
    Posts
    29
    Rep Power
    0

    Default

    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:
    Java Code:
    SimpleDateFormat sdfC = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
    **Date myDate = sdfC.parse(request.getParameter("date"+Integer.toString(j)));
    Brings up the "java.lang.NullPointerException" from earlier, on the line with the stars.

    However this code:
    Java Code:
    SimpleDateFormat sdfC = new SimpleDateFormat("dd/MM/yyyy");
    Date myDate = sdfC.parse(request.getParameter("date"+Integer.toString(j)));
    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 constructor

    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Add Date Problem
    By UnfairBear in forum New To Java
    Replies: 2
    Last Post: 08-14-2011, 02:10 PM
  2. Add Date Problem
    By UnfairBear in forum Java Applets
    Replies: 0
    Last Post: 08-14-2011, 10:56 AM
  3. Add Date Problem
    By UnfairBear in forum Java Servlet
    Replies: 0
    Last Post: 08-13-2011, 09:33 PM
  4. sql date problem
    By realosso in forum New To Java
    Replies: 3
    Last Post: 06-04-2010, 04:42 AM
  5. date problem
    By karlkwanny in forum JDBC
    Replies: 1
    Last Post: 07-30-2009, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •