javadatejdbcprimefaces

Java.util.Date vs Java.sql.Date - Date value differs


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>

Solution

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