General discussion

Locked

java programming - time

By hamant5 ·
Hey does anybody know a simple way to call and display the system time from the pc using java code? I have to submit an assignment and part of it involves displaying the system time on a simple java program - i was able to track down one method in 'java.util.Date' but this displays both the date AND the time...any help?

This conversation is currently closed to new comments.

5 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by BFilmFan In reply to java programming - time

Free code for that function at http://www.javascriptkit.com/script/script2/servertime.shtml

Collapse -

by Martin Simons In reply to java programming - time

GregorianCalendar a = new GregorianCalendar( ) ;
System.out.println(a.get(Calendar.HOUR) + ":" +
( (a.get(Calendar.MINUTE) < 10) ? "0" : "") +
a.get(Calendar.MINUTE) +
( (a.get(Calendar.AM_PM) == Calendar.AM) ? "AM" : "PM") ;

Collapse -

by Martin Simons In reply to

or for a 24 hour clock use HOUR_OF_DAY instead of HOUR and throw away the AM/PM line.

Collapse -

by Martin Simons In reply to java programming - time

SimpleDateFormat sdf = new SimpleDateFormat("hh:mm a") ;
System.out.println(sdf.format(new Date());

SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm") ;
System.out.println(sdf.format(new Date());

Collapse -

by ganaparthi In reply to java programming - time

try using java.text.SimpleDateFormat to extract only time part.

Back to Web Development Forum
5 total posts (Page 1 of 1)  

Related Discussions

Related Forums