0

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>
JeroenVP
  • 175
  • 2
  • 12

3 Answers3

2

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.

Lokesh
  • 7,810
  • 6
  • 48
  • 78
  • I am not getting proper formatting in above answer. You can follow this link also: http://docs.oracle.com/javase/1.5.0/docs/guide/jdbc/getstart/mapping.html – Lokesh Apr 18 '13 at 12:47
  • In my JavaClass I'm getting Date dteBeginOrWaitingItem = (util.sql.date) 2010-09-16); so the time is already lost right? Hibernate is communicating with my database so I have no idea how to fix this.. – JeroenVP Apr 18 '13 at 13:01
  • You are reading data from Database right ? and i believe using hibernate you must be populating a bean after query data from DB, right? Just make your date as java.util.Date in your bean – Lokesh Apr 18 '13 at 13:04
  • I've added my bean, as you can see I have the java.util.date import also declaring my dteBeginOrWaitingItem as a Date object. Also in orWaitinglist class the .getWPlannedDate is declared as a java.util.Date – JeroenVP Apr 18 '13 at 13:11
  • dteBeginOrWaitingItem = new Date(orWaitinglist.getWPlannedDate().getTime()); again no time is inside the value :( – JeroenVP Apr 18 '13 at 14:39
0

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());
Gokhan Demirhan
  • 1,099
  • 10
  • 14
  • Well as I said, there is no time in my date variable so this is kinda useless as it just returns me the date with 0 time: 12/02/12-00:00... – JeroenVP Apr 18 '13 at 14:08
  • if you are using .xml for hibernate change type to timestamp instead of date. `` therefore you have time on your database and can use simpledateformat – Gokhan Demirhan Apr 18 '13 at 14:43
  • omg it worked! :D will hibernate or java make any problems out of this when i try to write in this table now? now only 1 more problem my time in the database is 8:45 but in my variable its 7:45, i think this has something to do with "CET" timezone, any fix for that? – JeroenVP Apr 18 '13 at 14:51
  • I edited original answer to give exact solution to your question. Just try and see if there will be problem when writing to table, but it will work fine. To your second question: try `dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));` referring to this post: [link](http://stackoverflow.com/questions/2891361/java-how-to-set-timezone-of-a-java-util-date) – Gokhan Demirhan Apr 18 '13 at 17:27
0

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197