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();
}
}
-
Java is NOT JavaScript.Cerbrus– Cerbrus2014-09-19 06:54:52 +00:00Commented Sep 19, 2014 at 6:54
Add a comment
|
1 Answer
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
10 Comments
atf
excuse me but i want to just time
Ruchira Gayan Ranaweera
@atf read my edit. You can get any part form calender.
atf
excuse me but i want to just time
Ruchira Gayan Ranaweera
@atf
12:32:08 is time. What is your expected out put?atf
Gayan Ranaweei i want to a program that just time
|