Timestamp

Hi,

How can I returnonly the date without the time?

publicstatic Timestamp newDate(){

returnnew Timestamp(new Date().getTime());

}

[389 byte] By [yael800a] at [2007-11-26 21:12:23]
# 1
Construct a GregorianCalendar from the new Date() call, use the .set method to clear the Calendar.HOURS, MINUTES, SECONDS, MILLISECONDS fields, then convert the calendar into a Timestamp
dmbdmba at 2007-7-10 2:50:12 > top of Java-index,Java Essentials,Java Programming...
# 2

public static java.sql.Date newDate() {

return new java.sql.Date(new java.util.Date().getTime());

}

DrClapa at 2007-7-10 2:50:12 > top of Java-index,Java Essentials,Java Programming...
# 3

Like this?

public static Timestamp newDate() {

Date date = new Date();

Calendar cal = new GregorianCalendar();

cal.setTime(date);

cal.set(Calendar.HOUR_OF_DAY, 23);

cal.set(Calendar.MINUTE, 59);

cal.set(Calendar.SECOND, 0);

cal.set(Calendar.MILLISECOND, 0);

return new Timestamp(cal.getTime().getTime());

// return new Timestamp(new Date().getTime());

}

yael800a at 2007-7-10 2:50:12 > top of Java-index,Java Essentials,Java Programming...
# 4
cal.set(Calendar.HOUR_OF_DAY, 23);cal.set(Calendar.MINUTE, 59);?why are you not using 0?
dmbdmba at 2007-7-10 2:50:13 > top of Java-index,Java Essentials,Java Programming...
# 5
A timestamp with a day-wide granularity doesn't seem very useful.
paulcwa at 2007-7-10 2:50:13 > top of Java-index,Java Essentials,Java Programming...
# 6
I have a query with:SELECT * FROM tableName WHERE date <= '"+ datePicker +"';I want to select until 12/5/2007 --> so I have to set the our 23:59 to include all howers of this day, becaus if I not do this, if I selected "today" --> then the hower is "now"
yael800a at 2007-7-10 2:50:13 > top of Java-index,Java Essentials,Java Programming...
# 7
i see. or you could clear all the hours,mins,secs, millis, add one to the day and use <
dmbdmba at 2007-7-10 2:50:13 > top of Java-index,Java Essentials,Java Programming...
# 8
oh and by the way, you should use a PreparedStatement, and not rely on the toString of Timestamp.
dmbdmba at 2007-7-10 2:50:13 > top of Java-index,Java Essentials,Java Programming...
# 9

But, I stuil have a problem:

My goal is to move the houers from the datepicker to get only the date. this give my "today", I want to leave the day that selected without the time

GregorianCalendar cal = new GregorianCalendar();

cal.set(Calendar.HOUR_OF_DAY, 0);

cal.set(Calendar.MINUTE, 0);

cal.set(Calendar.SECOND, 0);

cal.set(Calendar.MILLISECOND, 0);

myDatePicker.setDate(cal.getTime());

Maybe something like:

myDatePicker.getDate().setHours(0);

myDatePicker.getDate().setMinutes(0);

myDatePicker.getDate().setSeconds(0);

Message was edited by:

yael800

yael800a at 2007-7-10 2:50:13 > top of Java-index,Java Essentials,Java Programming...
# 10
An object that represents midnight on a particular day is not the same thing as an object that represents the particular day in general.
paulcwa at 2007-7-10 2:50:13 > top of Java-index,Java Essentials,Java Programming...