Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Joda-Time</h1> <hr> <p>UPDATE: The <a href="http://www.joda.org/joda-time/" rel="nofollow noreferrer"><em>Joda-Time</em></a> project is now in <a href="https://en.wikipedia.org/wiki/Maintenance_mode" rel="nofollow noreferrer">maintenance mode</a>, with the team advising migration to the <a href="http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html" rel="nofollow noreferrer"><em>java.time</em></a> classes. See <a href="https://docs.oracle.com/javase/tutorial/datetime/TOC.html" rel="nofollow noreferrer">Tutorial by Oracle</a>.</p> <p>See <a href="https://stackoverflow.com/a/50894645/642706">my other Answer</a> using the industry-leading <em>java.time</em> classes.</p> <hr> <p>Normally we consider it bad form on StackOverflow.com to answer a specific question by suggesting an alternate technology. But in the case of the date, time, and calendar classes bundled with Java 7 and earlier, those classes are so notoriously bad in both design and execution that I am compelled to suggest using a 3rd-party library instead: <a href="http://www.joda.org/joda-time/" rel="nofollow noreferrer">Joda-Time</a>.</p> <p>Joda-Time works by creating <a href="https://en.wikipedia.org/wiki/Immutable_object" rel="nofollow noreferrer">immutable objects</a>. So rather than alter the time zone of a DateTime object, we simply instantiate a new DateTime with a different time zone assigned.</p> <p>Your central concern of using both local and UTC time is so very simple in Joda-Time, taking just 3 lines of code.</p> <pre><code> org.joda.time.DateTime now = new org.joda.time.DateTime(); System.out.println( "Local time in ISO 8601 format: " + now + " in zone: " + now.getZone() ); System.out.println( "UTC (Zulu) time zone: " + now.toDateTime( org.joda.time.DateTimeZone.UTC ) ); </code></pre> <p>Output when run on the west coast of North America might be:</p> <p><code>Local time in ISO 8601 format: 2013-10-15T02:45:30.801-07:00</code></p> <p><code>UTC (Zulu) time zone: 2013-10-15T09:45:30.801Z</code></p> <p>Here is a class with several examples and further comments. Using Joda-Time 2.5.</p> <pre><code>/** * Created by Basil Bourque on 2013-10-15. * © Basil Bourque 2013 * This source code may be used freely forever by anyone taking full responsibility for doing so. */ public class TimeExample { public static void main(String[] args) { // Joda-Time - The popular alternative to Sun/Oracle's notoriously bad date, time, and calendar classes bundled with Java 8 and earlier. // http://www.joda.org/joda-time/ // Joda-Time will become outmoded by the JSR 310 Date and Time API introduced in Java 8. // JSR 310 was inspired by Joda-Time but is not directly based on it. // http://jcp.org/en/jsr/detail?id=310 // By default, Joda-Time produces strings in the standard ISO 8601 format. // https://en.wikipedia.org/wiki/ISO_8601 // You may output to strings in other formats. // Capture one moment in time, to be used in all the examples to follow. org.joda.time.DateTime now = new org.joda.time.DateTime(); System.out.println( "Local time in ISO 8601 format: " + now + " in zone: " + now.getZone() ); System.out.println( "UTC (Zulu) time zone: " + now.toDateTime( org.joda.time.DateTimeZone.UTC ) ); // You may specify a time zone in either of two ways: // • Using identifiers bundled with Joda-Time // • Using identifiers bundled with Java via its TimeZone class // ----| Joda-Time Zones |--------------------------------- // Time zone identifiers defined by Joda-Time… System.out.println( "Time zones defined in Joda-Time : " + java.util.Arrays.toString( org.joda.time.DateTimeZone.getAvailableIDs().toArray() ) ); // Specify a time zone using DateTimeZone objects from Joda-Time. // http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTimeZone.html org.joda.time.DateTimeZone parisDateTimeZone = org.joda.time.DateTimeZone.forID( "Europe/Paris" ); System.out.println( "Paris France (Joda-Time zone): " + now.toDateTime( parisDateTimeZone ) ); // ----| Java Zones |--------------------------------- // Time zone identifiers defined by Java… System.out.println( "Time zones defined within Java : " + java.util.Arrays.toString( java.util.TimeZone.getAvailableIDs() ) ); // Specify a time zone using TimeZone objects built into Java. // http://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html java.util.TimeZone parisTimeZone = java.util.TimeZone.getTimeZone( "Europe/Paris" ); System.out.println( "Paris France (Java zone): " + now.toDateTime(org.joda.time.DateTimeZone.forTimeZone( parisTimeZone ) ) ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload