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();
    }
}
1
  • Java is NOT JavaScript. Commented Sep 19, 2014 at 6:54

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

10 Comments

excuse me but i want to just time
@atf read my edit. You can get any part form calender.
excuse me but i want to just time
@atf 12:32:08 is time. What is your expected out put?
Gayan Ranaweei i want to a program that just time
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.