0
import java.util.Date;
public class Time {  
    public static void main(String args[]) {   
        Date myDate=new Date();
        int hours=myDate.getHours();
        int minutes=myDate.getMinutes();
        int seconds=myDate.getSeconds();
    }
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
atf
  • 11
  • 1

1 Answers1

1

You can use Calendar for this. Don't use java.util.Date for getHours() since those are deprecated.

  Calendar calendar=Calendar.getInstance();
  System.out.println(calendar.getTime()); // get time will give you current time

Out put:

  Fri Sep 19 12:23:48 IST 2014

You should read about Calendar, you can find all you need there.

For your comment. If you only want Time part you can use DateFormat

Eg:

Calendar calendar=Calendar.getInstance();    
DateFormat df=new SimpleDateFormat("HH:mm:ss");
System.out.println(df.format(calendar.getTime()));

Out put:

12:32:08

Or you can get current hour, minute and seconds too.

Eg:

Calendar calendar=Calendar.getInstance();
System.out.println("current hour: "+calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("current minute: "+calendar.get(Calendar.MINUTE));
System.out.println("current second: " +calendar.get(Calendar.SECOND));

Out put:

current hour: 12
current minute: 48
current second: 8
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115