I need to Strip a Date from a Calendar. I have something like that: 'DD/MM/yyyy HH:mm:ss' but I need just the time ('HH:mm:ss').
Asked
Active
Viewed 243 times
2 Answers
5
You probably want to format the date, you can use SimpleDateFormat:
System.out.println(new SimpleDateFormat("HH:mm:ss")
.format(Calendar.getInstance().getTime()));
this prints something like:
20:20:11
EDIT
I suggest you use java.sql.Time and a PreparedStatement#setTime to build your criteria
Calendar cal = Calendar.getInstance();
Time time = new Time(cal.getTime().getTime());
pst = con.prepareStatement("select * from mytable where t=?");
pst.setTime(1, time);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println(rs.getTime("t"));
}
-
-
@iknunes how to you save the time in the data base/ which type has the table column? – A4L Nov 12 '13 at 17:32
-
It's a TIME. I have a Calendar with 'DD/MM/yyyy HH:mm:ss' then I need a DATE with 'HH:mm:ss' to compare in my criteria. – iknunes Nov 12 '13 at 18:24
0
The below code would strip off DD/MM/yyyy and print only the Hour Minute and seconds.
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class JavaUtilTimeTester {
public static void main(String[] args) {
Calendar cal=Calendar.getInstance();
System.out.println("Calendar:"+cal.toString());
Date d=cal.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("DD/MM/yyyy HH:mm:ss");
System.out.println(sdf.format(d));
SimpleDateFormat sdfNew=new SimpleDateFormat("HH:mm:ss");
System.out.println(sdfNew.format(d));
}
}

Thiru
- 11
- 3