• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp;
33 
34 import static org.threeten.bp.temporal.ChronoField.DAY_OF_MONTH;
35 import static org.threeten.bp.temporal.ChronoField.DAY_OF_WEEK;
36 import static org.threeten.bp.temporal.ChronoField.EPOCH_DAY;
37 
38 import java.io.PrintStream;
39 import java.util.Set;
40 
41 import org.threeten.bp.chrono.Chronology;
42 import org.threeten.bp.chrono.ChronoLocalDate;
43 import org.threeten.bp.chrono.HijrahChronology;
44 import org.threeten.bp.chrono.IsoChronology;
45 import org.threeten.bp.chrono.JapaneseChronology;
46 import org.threeten.bp.chrono.MinguoChronology;
47 import org.threeten.bp.chrono.ThaiBuddhistChronology;
48 import org.threeten.bp.temporal.ChronoField;
49 import org.threeten.bp.temporal.ChronoUnit;
50 import org.threeten.bp.temporal.JulianFields;
51 
52 /**
53  * Usability class for package.
54  */
55 public final class UsabilityChrono {
56 
main(String[] args)57     public static void main(String[] args) {
58         System.out.println("------");
59         newPackagePluggable();
60         System.out.println("------");
61         epochDays();
62         System.out.println("------");
63         printMinguoCal();
64         System.out.println("------");
65         example1();
66     }
67 
UsabilityChrono()68     private UsabilityChrono() {
69     }
70 
71     static {
72         Chronology c = JapaneseChronology.INSTANCE;
73         c = MinguoChronology.INSTANCE;
74         c = ThaiBuddhistChronology.INSTANCE;
75         c = JapaneseChronology.INSTANCE;
76         c = MinguoChronology.INSTANCE;
77         c = HijrahChronology.INSTANCE;
78         c = IsoChronology.INSTANCE;
c.toString()79         c.toString();
80     }
81 
newPackagePluggable()82     private static void newPackagePluggable() {
83         Chronology chrono = MinguoChronology.INSTANCE;
84 
85         ChronoLocalDate date = chrono.dateNow();
86         System.out.printf("now: %s%n", date);
87 
88         date = date.with(DAY_OF_MONTH, 1);
89         System.out.printf("first of month: %s%n", date);
90 
91         int month = (int) date.get(ChronoField.MONTH_OF_YEAR);
92         date = date.with(DAY_OF_WEEK, 1);
93         System.out.printf("start of first week: %s%n", date);
94 
95         while (date.get(ChronoField.MONTH_OF_YEAR) <= month) {
96             String row = "";
97             for (int i = 0; i < 7; i++) {
98                 row += date.get(ChronoField.DAY_OF_MONTH) + " ";
99                 date = date.plus(1, ChronoUnit.DAYS);
100             }
101             System.out.println(row);
102         }
103     }
104 
epochDays()105     private static void epochDays() {
106         output(LocalDate.now());
107         output(LocalDate.of(1945, 11, 12));
108         output(LocalDate.of(-4713, 11, 24));
109         output(LocalDate.of(1858, 11, 17));
110         output(LocalDate.of(1970, 1, 1));
111         output(LocalDate.of(1, 1, 1));
112     }
113 
output(LocalDate date)114     protected static void output(LocalDate date) {
115         System.out.println(date);
116         System.out.println("EPOCH_DAY " + date.getLong(EPOCH_DAY));
117         System.out.println("JDN " + date.getLong(JulianFields.JULIAN_DAY));
118         System.out.println("MJD " + date.getLong(JulianFields.MODIFIED_JULIAN_DAY));
119         System.out.println("RD  " + date.getLong(JulianFields.RATA_DIE));
120         System.out.println();
121     }
122 
123 
124     /**
125      * Example code.
126      */
example1()127     static void example1() {
128         System.out.printf("Available Calendars%n");
129 
130         // Print the Minguo date
131         ChronoLocalDate now1 = MinguoChronology.INSTANCE.dateNow();
132         int day = now1.get(ChronoField.DAY_OF_MONTH);
133         int dow = now1.get(ChronoField.DAY_OF_WEEK);
134         int month = now1.get(ChronoField.MONTH_OF_YEAR);
135         int year = now1.get(ChronoField.YEAR);
136         System.out.printf("  Today is %s %s %d-%s-%d%n", now1.getChronology().getId(),
137                 DayOfWeek.of(dow), year, month, day);
138 
139         // Print today's date and the last day of the year for the Minguo Calendar.
140         ChronoLocalDate first = now1
141                 .with(ChronoField.DAY_OF_MONTH, 1)
142                 .with(ChronoField.MONTH_OF_YEAR, 1);
143         ChronoLocalDate last = first
144                 .plus(1, ChronoUnit.YEARS)
145                 .minus(1, ChronoUnit.DAYS);
146         System.out.printf("  1st of year: %s; end of year: %s%n", first, last);
147 
148         // Enumerate the list of available calendars and print today for each
149         LocalDate  before = LocalDate.of(-500, 1, 1);
150         Set<Chronology> chronos = Chronology.getAvailableChronologies();
151         for (Chronology chrono : chronos) {
152             ChronoLocalDate date = chrono.dateNow();
153             ChronoLocalDate date2 = chrono.date(before);
154             System.out.printf("   %20s: %22s, %22s%n", chrono.getId(), date, date2);
155         }
156     }
157 
158     /**
159      * Prints a Minguo calendar for the current month.
160      */
printMinguoCal()161     private static void printMinguoCal() {
162         String chronoName = "Minguo";
163         Chronology chrono = Chronology.of(chronoName);
164         ChronoLocalDate today = chrono.dateNow();
165         printMonthCal(today, System.out);
166     }
167 
168     /**
169      * Print a month calendar with complete week rows.
170      * @param date A date in some calendar
171      * @param out a PrintStream
172      */
printMonthCal(ChronoLocalDate date, PrintStream out)173     private static void printMonthCal(ChronoLocalDate date, PrintStream out) {
174 
175         int lengthOfMonth = (int) date.lengthOfMonth();
176         ChronoLocalDate end = date.with(ChronoField.DAY_OF_MONTH, lengthOfMonth);
177         end = end.plus(7 - end.get(ChronoField.DAY_OF_WEEK), ChronoUnit.DAYS);
178         // Back up to the beginning of the week including the 1st of the month
179         ChronoLocalDate start = date.with(ChronoField.DAY_OF_MONTH, 1);
180         start = start.minus(start.get(ChronoField.DAY_OF_WEEK), ChronoUnit.DAYS);
181 
182         out.printf("%9s Month %2d, %4d%n", date.getChronology().getId(),
183                 date.get(ChronoField.MONTH_OF_YEAR),
184                 date.get(ChronoField.YEAR));
185         String[] colText = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
186         printMonthRow(colText, " ", out);
187 
188         String[] cell = new String[7];
189         for ( ; start.compareTo(end) <= 0; start = start.plus(1, ChronoUnit.DAYS)) {
190             int ndx = start.get(ChronoField.DAY_OF_WEEK) - 1;
191             cell[ndx] = Integer.toString((int) start.get(ChronoField.DAY_OF_MONTH));
192             if (ndx == 6) {
193                 printMonthRow(cell, "|", out);
194             }
195         }
196     }
197 
printMonthRow(String[] cells, String delim, PrintStream out)198     private static void printMonthRow(String[] cells, String delim, PrintStream out) {
199         for (int i = 0; i < cells.length; i++) {
200             out.printf("%s%3s ", delim, cells[i]);
201         }
202         out.println(delim);
203     }
204 
205 }
206