001: /*
002: * Copyright 2001-2005 Stephen Colebourne
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.joda.example.time;
017:
018: import java.util.Locale;
019:
020: import org.joda.time.DateTime;
021: import org.joda.time.Instant;
022:
023: /**
024: * Example code demonstrating how to use Joda-Time.
025: *
026: * @author Stephen Colebourne
027: */
028: public class Examples {
029:
030: public static void main(String[] args) throws Exception {
031: try {
032: new Examples().run();
033: } catch (Throwable ex) {
034: ex.printStackTrace();
035: }
036: }
037:
038: private void run() {
039: runInstant();
040: System.out.println();
041: runDateTime();
042: System.out.println();
043: }
044:
045: private void runInstant() {
046: System.out.println("Instant");
047: System.out.println("=======");
048: System.out
049: .println("Instant stores a point in the datetime continuum as millisecs from 1970-01-01T00:00:00Z");
050: System.out.println("Instant is immutable and thread-safe");
051: System.out.println(" in = new Instant()");
052: Instant in = new Instant();
053: System.out
054: .println("Millisecond time: in.getMillis(): "
055: + in.getMillis());
056: System.out
057: .println("ISO string version: in.toString(): "
058: + in.toString());
059: System.out
060: .println("ISO chronology: in.getChronology(): "
061: + in.getChronology());
062: System.out
063: .println("UTC time zone: in.getDateTimeZone(): "
064: + in.getZone());
065: System.out
066: .println("Change millis: in.withMillis(0): "
067: + in.withMillis(0L));
068: System.out.println("");
069: System.out
070: .println("Convert to Instant: in.toInstant(): "
071: + in.toInstant());
072: System.out
073: .println("Convert to DateTime: in.toDateTime(): "
074: + in.toDateTime());
075: System.out
076: .println("Convert to MutableDT: in.toMutableDateTime(): "
077: + in.toMutableDateTime());
078: System.out
079: .println("Convert to Date: in.toDate(): "
080: + in.toDate());
081: System.out.println("");
082: System.out
083: .println(" in2 = new Instant(in.getMillis() + 10)");
084: Instant in2 = new Instant(in.getMillis() + 10);
085: System.out
086: .println("Equals ms and chrono: in.equals(in2): "
087: + in.equals(in2));
088: System.out
089: .println("Compare millisecond: in.compareTo(in2): "
090: + in.compareTo(in2));
091: System.out
092: .println("Compare millisecond: in.isEqual(in2): "
093: + in.isEqual(in2));
094: System.out
095: .println("Compare millisecond: in.isAfter(in2): "
096: + in.isAfter(in2));
097: System.out
098: .println("Compare millisecond: in.isBefore(in2): "
099: + in.isBefore(in2));
100: }
101:
102: private void runDateTime() {
103: System.out.println("DateTime");
104: System.out.println("=======");
105: System.out
106: .println("DateTime stores a the date and time using millisecs from 1970-01-01T00:00:00Z internally");
107: System.out.println("DateTime is immutable and thread-safe");
108: System.out.println(" in = new DateTime()");
109: DateTime in = new DateTime();
110: System.out
111: .println("Millisecond time: in.getMillis(): "
112: + in.getMillis());
113: System.out
114: .println("ISO string version: in.toString(): "
115: + in.toString());
116: System.out
117: .println("ISO chronology: in.getChronology(): "
118: + in.getChronology());
119: System.out
120: .println("Your time zone: in.getDateTimeZone(): "
121: + in.getZone());
122: System.out
123: .println("Change millis: in.withMillis(0): "
124: + in.withMillis(0L));
125: System.out.println("");
126: System.out
127: .println("Get year: in.getYear(): "
128: + in.getYear());
129: System.out
130: .println("Get monthOfYear: in.getMonthOfYear(): "
131: + in.getMonthOfYear());
132: System.out
133: .println("Get dayOfMonth: in.getDayOfMonth(): "
134: + in.getDayOfMonth());
135: System.out.println("...");
136: System.out
137: .println("Property access: in.dayOfWeek().get(): "
138: + in.dayOfWeek().get());
139: System.out
140: .println("Day of week as text: in.dayOfWeek().getAsText(): "
141: + in.dayOfWeek().getAsText());
142: System.out
143: .println("Day as short text: in.dayOfWeek().getAsShortText(): "
144: + in.dayOfWeek().getAsShortText());
145: System.out
146: .println("Day in french: in.dayOfWeek().getAsText(Locale.FRENCH):"
147: + in.dayOfWeek().getAsText(Locale.FRENCH));
148: System.out
149: .println("Max allowed value: in.dayOfWeek().getMaximumValue(): "
150: + in.dayOfWeek().getMaximumValue());
151: System.out
152: .println("Min allowed value: in.dayOfWeek().getMinimumValue(): "
153: + in.dayOfWeek().getMinimumValue());
154: System.out
155: .println("Copy & set to Jan: in.monthOfYear().setCopy(1): "
156: + in.monthOfYear().setCopy(1));
157: System.out
158: .println("Copy & add 14 months: in.monthOfYear().addCopy(14): "
159: + in.monthOfYear().addToCopy(14));
160: System.out
161: .println("Add 14 mnths in field:in.monthOfYear().addWrapFieldCopy(14): "
162: + in.monthOfYear().addWrapFieldToCopy(14));
163: System.out.println("...");
164: System.out
165: .println("Convert to Instant: in.toInstant(): "
166: + in.toInstant());
167: System.out
168: .println("Convert to DateTime: in.toDateTime(): "
169: + in.toDateTime());
170: System.out
171: .println("Convert to MutableDT: in.toMutableDateTime(): "
172: + in.toMutableDateTime());
173: System.out
174: .println("Convert to Date: in.toDate(): "
175: + in.toDate());
176: System.out
177: .println("Convert to Calendar: in.toCalendar(Locale.UK): "
178: + in.toCalendar(Locale.UK).toString()
179: .substring(0, 46));
180: System.out
181: .println("Convert to GregCal: in.toGregorianCalendar(): "
182: + in.toGregorianCalendar().toString()
183: .substring(0, 46));
184: System.out.println("");
185: System.out
186: .println(" in2 = new DateTime(in.getMillis() + 10)");
187: DateTime in2 = new DateTime(in.getMillis() + 10);
188: System.out
189: .println("Equals ms and chrono: in.equals(in2): "
190: + in.equals(in2));
191: System.out
192: .println("Compare millisecond: in.compareTo(in2): "
193: + in.compareTo(in2));
194: System.out
195: .println("Compare millisecond: in.isEqual(in2): "
196: + in.isEqual(in2));
197: System.out
198: .println("Compare millisecond: in.isAfter(in2): "
199: + in.isAfter(in2));
200: System.out
201: .println("Compare millisecond: in.isBefore(in2): "
202: + in.isBefore(in2));
203: }
204:
205: }
|