AchillesVan
AchillesVan

Reputation: 4356

SQL is returning only the year, not the day nor the month

I'm working on a small personal project as a Java student and i've been asked to create a simple webpage that displays a Mysql database. In mySql, i declared my dates in type DATE. See screanShot below.

enter image description here

The java code below shows how i retrieve the datas from my DB.

private Destination resultSetRowToCursist(ResultSet resultSet)
            throws SQLException {
        return new Destination (resultSet.getInt("CountryID"),
                resultSet.getString("Destination"),
                resultSet.getDate("DepartureDate"),
                resultSet.getDate("ReturnDate"), 
                resultSet.getInt("Price"),
                resultSet.getInt("AvailableSeats"));
    }

Below is the screanshot of the output on my webpage. DepartureDate and ReturnDate format on my webpage should be reflecting the same format as in the DB

enter image description here

This is my JSP code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="styles/default.css">

    <title>Travels</title>
    </head>
    <body>

    <table border=1>

            <tr>

                <th>CountryId</th>
                <th>Country</th>
                <th>DepartureDate</th>

                <th>ReturnDate</th>

                <th>Price</th>
                <th>AvailableSeats</th>

    </tr>

            <c:forEach var="destinationArrayListItem" items="${DestinationArrayList}">

                <tr>

                    <td>${destinationArrayListItem.countryID}</td>
                    <td>${destinationArrayListItem.destination}</td>
                    <td>${destinationArrayListItem.departureDate}</td>
                    <td>${destinationArrayListItem.returnDate}</td>
                    <td>${destinationArrayListItem.price}</td>
                    <td>${destinationArrayListItem.availableSeats}</td>


                </tr>

            </c:forEach>

        </table>



        <br />

        <c:url var="index" value="/IndexServlet" />

        <a class="HPbutton" href="${index}">Home Page</a>


    </body>
    </html>

My Destination class with COnstructor and getters

import java.io.Serializable;
import java.sql.Date;

public class Destination implements Serializable {

    private static final long serialVersionUID = 1L;

    private int countryID;

    private String destination;

    private  Date departureDate;
    private  Date returnDate;
    private  int price;
    private  int availableSeats;


    public Destination () {
          this.countryID=0;;

          this.destination="geen";

          this.departureDate= null;
          this.returnDate= null;
          this.price=0;
          this.availableSeats=0;


    }

    public Destination(int countryID, String destination, Date departureDate,
            Date returnDate, int price, int availableSeats) {
        this.countryID = countryID;
        this.destination = destination;
        this.departureDate = departureDate;
        this.returnDate = returnDate;
        this.price = price;
        this.availableSeats = availableSeats;


    }

    public int getCountryID() {
        return countryID;
    }

    public void setCountryID(int countryID) {
        this.countryID = countryID;
    }

    public Date getDepartureDate() {
        return departureDate;
    }

    public void setDepartureDate(Date departureDate) {
        this.departureDate = departureDate;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public Date getReturnDate() {
        return returnDate;
    }

    public void setReturnDate(Date returnDate) {
        this.returnDate = returnDate;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getAvailableSeats() {
        return availableSeats;
    }

    public void setAvailableSeats(int availableSeats) {
        this.availableSeats = availableSeats;
    }


}

Upvotes: 1

Views: 222

Answers (1)

blackpanther
blackpanther

Reputation: 11486

Are you using javax.sql.Date or java.util.Date. Remebering that EL tags use the toString() method, it may be worth formatting the date as shown in the following thread:

Convert java.util.Date to String

Another way to do this is to use JSTL tags in the following manner:

<td><fmt:formatDate value="${destinationArrayListItem.departureDate}" pattern="MM/yyyy" /></td>
<td><fmt:formatDate value="${destinationArrayListItem.returnDate}" pattern="MM/yyyy" /></td>

To do this, you will have to add the JSTL library to your JavaEE application as follows:

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

Note, this is different from the JSTL core.

Upvotes: 1

Related Questions