alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (key_instant.xml)

This example Java source code file (key_instant.xml) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

api, compared, datetime, instant, jdk, joda-time, nulls, readableinstant, string, the, you

The key_instant.xml Java example source code

<?xml version="1.0" encoding="ISO-8859-1"?>

<document>

 <properties>
  <title>Java date and time API - Instant
  <author>Stephen Colebourne
 </properties>

 <body>

<section name="Instant">

<p>
The most frequently used concept in Joda-Time is that of the <i>instant.
An Instant is defined as <i>an instant in the datetime continuum specified as a
number of milliseconds from 1970-01-01T00:00Z</i>.
This definition of milliseconds is consistent with that of the JDK in <code>Date
or <code>Calendar. Interoperating between the two APIs is thus simple.
</p>
<p>
The millisecond instant can be converted to any date time field using a
<a href="key_chronology.html">Chronology.
To assist with this, methods are provided on <code>DateTime that act as getters for
the most common date and time fields.
More powerful access to the field can be obtained via its property.
<source>
DateTime dt = new DateTime();  // current time
int month = dt.getMonth();     // gets the current month
int month = dt.month().get();  // alternative way to get value
String monthStr = dt.month().getAsText();  // gets the month name
</source>
</p>
<p>
To deal with local times (no time zone), or with date only or time only concepts,
you should use the <a href="key_partial.html">partial classes.
</p>

</section>

<section name="Using Instants in Joda-Time">
<p>
Within Joda-Time an instant is represented by the
<a href="apidocs/org/joda/time/ReadableInstant.html">ReadableInstant interface.
There are four implementations of the interface provided:
<ul>
<li>Instant -
A simple immutable implementation which is restricted to the UTC time zone
and is intended for time zone and calendar neutral data transfer</li>
<li>DateTime -
The most commonly used class in the library, and an immutable representation of a
date and time with calendar and time zone</li>
<li>MutableDateTime -
A mutable representation of date and time with calendar and time zone</li>
<li>DateMidnight -
A deprecated implementation, similar to <code>DateTime but with the time component
forced to be midnight (at the start of a day)</li>
</ul>
We recommend the immutable implementations for general usage.
</p>
<p>
The code can be used in various ways:
<source>
// setup date object for midday on Christmas 2004 (ISO year 2004)
DateTime dt = new DateTime(2004, 12, 25, 12, 0, 0, 0);
// get the year, 2004
int year = dt.getYear();
// get the day of week index 1 (Monday) to 7 (Sunday)
int dow = dt.getDayOfWeek();
// get the text, such as 'Tuesday'
String dowStr = dt.dayOfWeek().getAsText();
</source>
Compared to <code>GregorianCalendar Joda-Time classes use 1-12 for months, and are
immutable in the standard implementations.
It is also easy to convert to and from the JDK classes.
<source>
// construct DateTime from JDK Date
Date jdkDate = new Date();
DateTime dt = new DateTime(jdkDate);

// construct Calendar from DateTime (could also construct a Date)
GregorianCalendar cal = dt.toGregorianCalendar();
</source>
</p>
<p>
Note that the interface <code>ReadableInstant should not be used like the collections API.
The interface only contains the core subset of the operations of <code>DateTime.
You should use the interface only when you feel the need to be flexible about future changes
to the object passed into a method.
You might also want to consider the <a href="apidocs/org/joda/time/ReadableDateTime.html">ReadableDateTime
interface which extends <code>ReadableInstant to provide additional methods.
</p>

<subsection name="Nulls">
<p>
Joda-Time defines a null instant as the current time.
Thus, when a method is defined as taking a <code>ReadableInstant, passing null in
will be the same as passing in an instant set to the current time.
</p>
</subsection>

</section>

</body>
</document>

Other Java examples (source code examples)

Here is a short list of links related to this Java key_instant.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.