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();
}
}
Asked
Active
Viewed 196 times
0
-
Java is NOT JavaScript. – Cerbrus Sep 19 '14 at 06:54
1 Answers
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
-
-
@atf read my edit. You can get any part form calender. – Ruchira Gayan Ranaweera Sep 19 '14 at 07:03
-
-
@atf `12:32:08` is time. What is your expected out put? – Ruchira Gayan Ranaweera Sep 19 '14 at 07:03
-
-
@atf time means hour and minutes ? Just show us in a example – Ruchira Gayan Ranaweera Sep 19 '14 at 07:10
-
-
-
-
@atf first read all linked article( `Java` docs). Then try to do something by your own. Then you will know how and why. – Ruchira Gayan Ranaweera Sep 19 '14 at 07:22