Difference between revisions of "Find the current system time"

From CodeCodex

m
 
(Implementations)
 
(5 intermediate revisions by 3 users not shown)
Line 12: Line 12:
 
     System.out.println( cal.get(Calendar.SECOND) );      // print the current second from 0-59
 
     System.out.println( cal.get(Calendar.SECOND) );      // print the current second from 0-59
 
</pre>
 
</pre>
 +
You can also call this static method which returns the time of the local system in "measured in milliseconds, between the current time and midnight, January 1, 1970 UTC."
 +
<pre>
 +
    System.currentTimeMillis()
 +
</pre>
 +
This can then be used to construct a Date object representing the current time.
 +
<pre>
 +
  Date now = new Date(System.currentTimeMillis())
 +
</pre>
 +
 +
 
Many more functions are listed in the Java [http://java.sun.com/j2se/1.5.0/docs/api/java/util/GregorianCalendar.html API].
 
Many more functions are listed in the Java [http://java.sun.com/j2se/1.5.0/docs/api/java/util/GregorianCalendar.html API].
 +
 +
===Ruby===
 +
<pre>
 +
time = Time.now
 +
</pre>
 +
See the Ruby [http://corelib.rubyonrails.org/classes/Time.html API] for functions available to time object.
 +
 +
=== Tcl ===
 +
<pre class="tcl">
 +
clock format [clock seconds]
 +
</pre>
 +
===Zsh===
 +
<pre>
 +
zmodload zsh/datetime
 +
strftime %c $EPOCHSECONDS
 +
</pre>
 +
 +
[[Category:Java]]
 +
[[Category:Ruby]]
 +
[[Category:Tcl]]
 +
[[Category:Zsh]]

Latest revision as of 14:40, 20 February 2011

Implementations[edit]

Java[edit]

When you instantiate a GregorianCalendar object in Java, it is created with the current time.

    Calendar cal = new GregorianCalendar();

Then you can use the Calendar object or its methods to get the time information you need.

    // Get the components of the time
    System.out.println( cal.get(Calendar.HOUR_OF_DAY) ); // print the current hour from 0-23
    System.out.println( cal.get(Calendar.MINUTE) );      // print the current minute from 0-59
    System.out.println( cal.get(Calendar.SECOND) );      // print the current second from 0-59

You can also call this static method which returns the time of the local system in "measured in milliseconds, between the current time and midnight, January 1, 1970 UTC."

    System.currentTimeMillis()

This can then be used to construct a Date object representing the current time.

   Date now = new Date(System.currentTimeMillis())


Many more functions are listed in the Java API.

Ruby[edit]

time = Time.now

See the Ruby API for functions available to time object.

Tcl[edit]

clock format [clock seconds]

Zsh[edit]

zmodload zsh/datetime
strftime %c $EPOCHSECONDS