Java.util.Date vs Java.sql.Date

I have an Oracle database which contains a field W_PLANNED_DATE: 19/03/2013 10:55:00 (Date)

In Java I put this value into a variable:

Date dteBeginOrWaitingItem = orWaitinglist.getWPlannedDate();

value: 2013-03-19

Now, what happend to my time? I need this to fill the schedulecomponent of primefaces. How can i get a full date and time value

eventResourceAvailPerDay.addEvent(new DefaultScheduleEvent(reason, dteBeginOrWaitingItem, dteEndOrWaitingItem, "waitingitem"));

This method puts the event at 12:00 PM as there is no other time or the time is just 00:00:00

I know how to use the Calendar class but it just lets me set the date, the time seems to be empty but in database view I have a date time value.

Mybean

import java.util.Date;
@ManagedBean(name="scheduleController")
@SessionScoped
public class ScheduleController implements Serializable {

private Date dteBeginOrWaitingItem, dteEndOrWaitingItem;

//methods
try
    {
        //eventWaitinglist.clear();
        OrWaitinglistDao orWaitinglistDao = new OrWaitinglistDaoImpl();
        waitingEvents = orWaitinglistDao.getOrWaitinglistKeysByResource(rKey);
        int i = 0;
        Iterator<OrWaitinglist> it2 = waitingEvents.iterator();
        while (it2.hasNext()) 
        {
            orWaitinglist = it2.next();
            dteBeginOrWaitingItem = (Date) orWaitinglist.getWPlannedDate();
            dteEndOrWaitingItem = orWaitinglist.getWPlannedDate();
            //dteEndOrWaitingItem = orWaitinglist.getWPlannedDate();
            reason = orWaitinglist.getWDescription();
            eventResourceAvailPerDay.addEvent(new DefaultScheduleEvent(reason, dteBeginOrWaitingItem, dteEndOrWaitingItem, "waitingitem"));

            i += 1;
            System.out.println("EventWaiting: " + i + " " + dteBeginOrWaitingItem + " " + dteEndOrWaitingItem + " " + reason);
        }
     }
    catch(java.util.EmptyStackException Ex)
    {
        System.out.println(Ex.getMessage());
    }

WORKING UPDATE: Bean:

try
    {
        //eventWaitinglist.clear();
        OrWaitinglistDao orWaitinglistDao = new OrWaitinglistDaoImpl();
        waitingEvents = orWaitinglistDao.getOrWaitinglistKeysByResource(rKey);
        int i = 0;
        Iterator<OrWaitinglist> it2 = waitingEvents.iterator();
        while (it2.hasNext()) 
        {
            orWaitinglist = it2.next();
            Long wPlannedDate = orWaitinglist.getWPlannedDate().getTime();
            if (wPlannedDate != 0) {
                Date wPlannedDateConverted = new Date(wPlannedDate);
                dteBeginOrWaitingItem = convertDate(0, 0, wPlannedDateConverted);
                dteEndOrWaitingItem = convertDate(orWaitinglist.getWDuration().intValue(), orWaitinglist.getWAdditionalTime().intValue(), wPlannedDateConverted);
            }
            reason = orWaitinglist.getWDescription();
            DefaultScheduleEvent newResourceEvent = new DefaultScheduleEvent(reason, dteBeginOrWaitingItem, dteEndOrWaitingItem, orWaitinglist);
            newResourceEvent.setStyleClass("waitingitem");
            eventResourceAvailPerDay.addEvent(newResourceEvent);
        }
     }
    catch(java.util.EmptyStackException Ex)
    {
        System.out.println(Ex.getMessage());
    }


public static Date convertDate(Integer wDuration, Integer wAdditionalTime, Date availDate)
{
    Calendar cal = Calendar.getInstance();
    Integer wAdditionalTimeHours, wAdditionalTimeMinutes;
    Integer wDurationHours, wDurationMinutes;

    if(wAdditionalTime != 0 || wDuration != 0) {
        if (wAdditionalTime !=0) {
            wAdditionalTimeHours = (int) Math.floor (wAdditionalTime / 60);
            wAdditionalTimeMinutes = wAdditionalTime - (wAdditionalTimeHours * 60);
            cal.setTime(availDate);
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
            cal.add(Calendar.MINUTE, wAdditionalTimeMinutes);
            cal.add(Calendar.HOUR_OF_DAY, wAdditionalTimeHours);
        }
        if (wDuration != 0) {
            wDurationHours = (int) Math.floor (wAdditionalTime / 60);
            wDurationMinutes = wAdditionalTime - (wDurationHours * 60);
            cal.setTime(availDate);
            cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
            cal.add(Calendar.MINUTE, wDurationMinutes);
            cal.add(Calendar.HOUR_OF_DAY, wDurationHours);
        }
    } else {
        cal.setTime(availDate);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
    }
    return cal.getTime();
}

Model update:

<property name="WPlannedDate" type="timestamp">
  <column length="7" name="W_PLANNED_DATE">
    <comment>Planned date</comment>
  </column>
</property>

EDIT: if you are using .xml for hibernate change type to timestamp instead of date. <property name="yourdate" column="YOUR_DATE" type="timestamp" /> therefore you have time on your database and can use simpledateformat

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy-HH:mm");
dateFormat.format(orWaitinglist.getWPlannedDate());

java.sql.Date will not return time component, you should use java.util.Date while creating your preparedstatement or any other way your are querying the db.

EDIT: Using java.util.Date with sql query can be tricky as query will expect java.sql.Date. It works for me in Spring. If you dont want to use this then you can use java.sql.Timestamp also.

See below documentation:

8.3.12 DATE, TIME, and TIMESTAMP

There are three JDBC types relating to time:

The JDBC DATE type represents a date consisting of day, month, and year. The corresponding SQL DATE type is defined in SQL-92, but it is implemented by only a subset of the major databases. Some databases offer alternative SQL types that support similar semantics. The JDBC TIME type represents a time consisting of hours, minutes, and seconds. The corresponding SQL TIME type is defined in SQL-92, but it is implemented by only a subset of the major databases. As with DATE, some databases offer alternative SQL types that support similar semantics. The JDBC TIMESTAMP type represents DATE plus TIME plus a nanosecond field. The corresponding SQL TIMESTAMP type is defined in SQL-92, but it is implemented by only a very small number of databases. Because the standard Java class java.util.Date does not match any of these three JDBC date/time types exactly (it includes both DATE and TIME information but has no nanoseconds), JDBC defines three subclasses of java.util.Date to correspond to the SQL types. They are:

java.sql.Date for SQL DATE information. The hour, minute, second, and millisecond fields of the java.util.Date base class should be set to zero. If the number of milliseconds supplied to the java.sql.Date constructor is negative, the driver will compute the date as the number of milliseconds before January 1, 1970. Otherwise, the date is computed as the specified number of milliseconds after January 1, 1970.

java.sql.Time for SQL TIME information. The year, month, and day fields of the java.util.Date base class are set to 1970, January, and 1. This is the "zero" date in the Java epoch. java.sql.Timestamp for SQL TIMESTAMP information. This class extends java.util.Date by adding a nanoseconds field.


Instead of java.sql.Date , java.sql.Timestamp and the matching methods should be used. The oracle type DATE is equivalent to the JDBC and SQL-standard type TIMESTAMP .

A driver is required by the JDBC spec to exclude the time part when one of the get/setDate methods are used.

链接地址: http://www.djcxy.com/p/3080.html

上一篇: 在Eclipse RCP中显示视图时传递参数

下一篇: Java.util.Date与Java.sql.Date