0

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').

iknunes
  • 13
  • 1
  • 7
  • 3
    http://stackoverflow.com/questions/1363307/getting-the-time-component-of-a-java-date-or-calendar – Habib Nov 11 '13 at 19:17

2 Answers2

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"));
}
Community
  • 1
  • 1
A4L
  • 17,353
  • 6
  • 49
  • 70
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