Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Tolls

Thread: SimpleDateFormat Problem

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

    Default SimpleDateFormat Problem

    EDIT: Discovered I was trying to format a number when I should have been formatting a date. Took a lot of code and work-arounds to create said date, but worked it out.

    Sorry for the clutter, delete if necessary!


    I'm trying to generate a string for each month in a school year working back from July with a wildcard as a Date to compare it in MySQL. My problem is that when I try to make the string, SimpleDateFormat doesn't seem to want to format the month, so I just end up getting records from January.

    Here's the appropriate code bit:
    Java Code:
    <c:forEach var="i" begin="0" end="6" step="1">
    
    						<c:set var="theMonth" value="${7-i}"/><c:out value="month: ${theMonth}"/></br>
    
    						<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    						SimpleDateFormat sdfB = new SimpleDateFormat("MM");
    						String yearString = sdf.format(pageContext.getAttribute("endYear"));
    						String monthString = sdfB.format(pageContext.getAttribute("theMonth"));
    						String date = new String(yearString+"-"+monthString+"-__");%><c:out value="<%=date%>"/></br></br>
    As you can see, I put in some test c:outs

    Here's the output:
    Java Code:
    month: 7
    2012-01-__
    
    month: 6
    2012-01-__
    
    month: 5
    2012-01-__
    
    month: 4
    2012-01-__
    
    month: 3
    2012-01-__
    
    month: 2
    2012-01-__
    
    month: 1
    2012-01-__
    
    month: 12
    2012-01-__
    
    month: 11
    2012-01-__
    
    month: 10
    2012-01-__
    
    month: 9
    2012-01-__
    So SimpleDateFormat seems to be formatting properly, but it just doesn't want to put the right number in. I even hard coded the number 12 into the format method and it still came out as 1. The year formatting is working fine. I also tried just doing the following:
    Java Code:
    int month = pageContext.getAttribute("month");
    ... but the month variable is an Object so it won't assign to an Int and ParseInt doesnt work either.

    The 'theMonth' variable seems to be passing into the Java code properly, because I tried this:
    Java Code:
    String date = new String(yearString+"-"+pageContext.getAttribute("month")+"-__");%>
    ... and it gave me an output of something like '2012-5-__'. The problem with this method is that I need the '0' before the '5' or it won't match the dates in my database, which is why I need to format the month.

    I'm pretty stumped, any help would be appreciated.

    And please please PLEASE don't just post saying "You shouldn't be using JSP for blah blah blah blah", it's extremely annoying and not helpful at all. </grouchy old woman>

    Here's the full code in case anyone was looking for it:
    Java Code:
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix = "c"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/sql"  prefix = "sql"%>
    <%@ page import="java.util.*" %>
    <%@ page import="java.text.*" %>
    
    <html>
    
        <sql:setDataSource
           var = "CoolSchool"
           driver = "com.mysql.jdbc.Driver"
           url = "jdbc:mysql://localhost:3306/CoolSchool"
           scope = "session"
    	user = "root"
    	password = "dbpw"
        />
    
        <body>
            <table width="100%" border=1>
                <tr><td colspan=2>
                    <%@include file = "header.jsp" %>
                </td></tr>
    
                <tr>    
    		        <td width = "20%" valign=top>
    		            <%@include file = "studentNav.jsp" %>
    		        </td>
    		        <td width = "*">
    
    				<sql:query var="getYear" dataSource="${CoolSchool}">
    					SELECT start_year, end_year from current_year
    					WHERE current = 'y'
    				</sql:query>
    				<c:forEach items="${getYear.rows}" var="row">
    					<c:set var="startYear" value="${row.start_year}"/>
    					<c:set var="endYear" value="${row.end_year}"/>
    				</c:forEach>
    
    				<table>
    					<tr>
    						<td><b>Date</b></td>
    						<td><b>Teacher Involved</b></td>
    						<td><b>Problem</b></td>
    						<td><b>Measures Taken</b></td>
    						<td><b>Edit</b></td>
    					</tr>
    
    					<c:forEach var="i" begin="0" end="6" step="1">
    
    						<c:set var="theMonth" value="${7-i}"/><c:out value="month: ${theMonth}"/></br>
    
    						<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    						SimpleDateFormat sdfB = new SimpleDateFormat("MM");
    						String yearString = sdf.format(pageContext.getAttribute("endYear"));
    						String monthString = sdfB.format(pageContext.getAttribute("theMonth"));
    						String date = new String(yearString+"-"+monthString+"-__");%><c:out value="<%=date%>"/></br></br>
    
    
    
    
    						<sql:query var="studentHealth" dataSource="${CoolSchool}">
    							SELECT * from health_record JOIN teacher ON health_record.teacher=teacher.teacher_id
    							WHERE date like ?
    							<sql:param value="<%=date%>"/>
    						</sql:query>
    
    						<c:forEach items="${studentHealth.rows}" var="row">
    
    							<tr>
    								<td><c:out value="${row.date}"/></td>
    								<td><c:out value="${row.first_name} ${row.second_name}"/></td>
    								<td><c:out value="${row.problem}"/></td>
    								<td><c:out value="${row.measures_taken}"/></td>
    								<td><form method=post action="editHealthRecord.jsp">
    									<input type="hidden" name="teacher" value="${row.teacher}">
    									<input type="hidden" name="date" value="${row.date}">
    									<input type="hidden" name="problem" value="${row.problem}">
    									<input type="hidden" name="measuresTaken" value="${row.measures_taken}">
    									<input type="hidden" name="student" value="${row.student}">
    									<input type="hidden" name="healthID" value="${row.health_id}">
    									<input type="submit" value="Edit">
    								</form></td>
    							</tr>
    			
    						</c:forEach>
    					</c:forEach>
    
    					<c:forEach var="i" begin="0" end="3" step="1">
    
    						<c:set var="theMonth" value="${12-i}"/><c:out value="month: ${theMonth}"/></br>
    
    						<%SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    						SimpleDateFormat sdfB = new SimpleDateFormat("MM");
    						String yearString = sdf.format(pageContext.getAttribute("endYear"));
    						String monthString = sdfB.format(pageContext.getAttribute("theMonth"));
    						String date = new String(yearString+"-"+monthString+"-__");%><c:out value="<%=date%>"/></br></br>
    
    
    						<sql:query var="studentHealth" dataSource="${CoolSchool}">
    							SELECT * from health_record JOIN teacher ON health_record.teacher=teacher.teacher_id
    							WHERE date like ?
    							<sql:param value="<%=date%>"/>
    						</sql:query>
    
    						<c:forEach items="${studentHealth.rows}" var="row">
    
    							<tr>
    								<td><c:out value="${row.date}"/></td>
    								<td><c:out value="${row.first_name} ${row.second_name}"/></td>
    								<td><c:out value="${row.problem}"/></td>
    								<td><c:out value="${row.measures_taken}"/></td>
    								<td><form method=post action="editHealthRecord.jsp">
    									<input type="hidden" name="teacher" value="${row.teacher}">
    									<input type="hidden" name="date" value="${row.date}">
    									<input type="hidden" name="problem" value="${row.problem}">
    									<input type="hidden" name="measuresTaken" value="${row.measures_taken}">
    									<input type="hidden" name="student" value="${row.student}">
    									<input type="hidden" name="healthID" value="${row.health_id}">
    									<input type="submit" value="Edit">
    								</form></td>
    							</tr>
    			
    						</c:forEach>
    					</c:forEach>
    				
    				</table>
    		            
    		        </td>
    		    
                </tr>
    
            </table>
        </body>
        
    </html>
    Last edited by UnfairBear; 09-10-2011 at 03:43 PM.

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

    Default Re: SimpleDateFormat Problem

    Quote Originally Posted by UnfairBear View Post
    And please please PLEASE don't just post saying "You shouldn't be using JSP for blah blah blah blah", it's extremely annoying and not helpful at all. </grouchy old woman>
    Well, it is actually helpful in that anyone else coming in looking for some help or other will know that using a JSP for this sort of thing is plain poor practice.
    We do realise that instructors all over the place seem to use JSPs as a training tool (a horrible mistake, frankly), but it should be pointed out as often as possible so people do not get the idea that this is the correct way of doing things.

    Anyway, good to hear you've sorted your problem out.
    Fubarable likes this.

Similar Threads

  1. Problem with SimpleDateFormat
    By johan313 in forum Advanced Java
    Replies: 3
    Last Post: 01-17-2011, 05:20 PM
  2. JTextField and SimpleDateFormat
    By mine0926 in forum New To Java
    Replies: 15
    Last Post: 07-06-2010, 03:10 AM
  3. JFormattedTextField + SimpleDateFormat
    By Ralphw in forum AWT / Swing
    Replies: 1
    Last Post: 07-07-2009, 10:53 AM
  4. Locale with SimpleDateFormat
    By swati.jyoti in forum New To Java
    Replies: 4
    Last Post: 07-02-2009, 02:34 PM
  5. Using SimpleDateFormat
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-18-2008, 08:33 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
  •