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.TemporalAdjusters.previousOrSame; 36 37 import org.threeten.bp.format.DateTimeFormatter; 38 import org.threeten.bp.format.DateTimeFormatterBuilder; 39 import org.threeten.bp.temporal.ChronoField; 40 import org.threeten.bp.temporal.ChronoUnit; 41 import org.threeten.bp.temporal.TemporalAccessor; 42 import org.threeten.bp.temporal.TemporalField; 43 44 /** 45 * Usability class for package. 46 */ 47 public final class UsabilityBasic { 48 main(String[] args)49 public static void main(String[] args) { 50 simpleCalendar(); 51 System.out.println("------"); 52 lookup(); 53 System.out.println("------"); 54 period(); 55 System.out.println("------"); 56 print1(); 57 System.out.println("------"); 58 print2(); 59 } 60 UsabilityBasic()61 private UsabilityBasic() { 62 } 63 simpleCalendar()64 private static void simpleCalendar() { 65 LocalDate date = LocalDate.now(); 66 System.out.println(date); 67 68 date = date.withDayOfMonth(1); 69 System.out.println(date); 70 71 int month = date.getMonth().getValue(); 72 date = date.with(previousOrSame(DayOfWeek.MONDAY)); 73 System.out.println(date); 74 75 while (date.getMonth().getValue() <= month) { 76 String row = ""; 77 for (int i = 0; i < 7; i++) { 78 row += date.getDayOfMonth() + " "; 79 date = date.plusDays(1); 80 } 81 System.out.println(row); 82 } 83 } 84 lookup()85 private static void lookup() { 86 LocalDate date = LocalDate.now(); 87 LocalTime time = LocalTime.now(); 88 LocalDateTime dateTime = LocalDateTime.now(); 89 // System.out.println(LocalDateField.DAY_OF_MONTH.getDateRules().get(date)); 90 // System.out.println(LocalDateField.MONTH_OF_YEAR.getDateRules().get(date)); 91 // System.out.println(LocalDateField.YEAR.getDateRules().get(date)); 92 // System.out.println(QuarterYearField.QUARTER_OF_YEAR.getDateRules().get(date)); 93 // System.out.println(QuarterYearField.MONTH_OF_QUARTER.getDateRules().get(date)); 94 // System.out.println(QuarterYearField.DAY_OF_QUARTER.getDateRules().get(date)); 95 96 output(date, ChronoField.DAY_OF_MONTH); 97 output(date, ChronoField.MONTH_OF_YEAR); 98 output(date, ChronoField.YEAR); 99 100 output(dateTime, ChronoField.DAY_OF_MONTH); 101 output(time, ChronoField.HOUR_OF_DAY); 102 output(time, ChronoField.MINUTE_OF_HOUR); 103 104 TemporalAccessor cal = date; 105 System.out.println("DoM: " + cal.get(DAY_OF_MONTH)); 106 } 107 output(LocalDate date, TemporalField field)108 protected static void output(LocalDate date, TemporalField field) { 109 System.out.println(field + " " + date.getLong(field)); 110 } 111 output(LocalDateTime dateTime, TemporalField field)112 protected static void output(LocalDateTime dateTime, TemporalField field) { 113 System.out.println(field + " " + dateTime.getLong(field)); 114 } 115 output(LocalTime time, TemporalField field)116 protected static void output(LocalTime time, TemporalField field) { 117 System.out.println(field + " " + time.getLong(field)); 118 } 119 period()120 private static void period() { 121 LocalDate date1 = LocalDate.now(); 122 LocalDate date2 = LocalDate.now().plusDays(25367); 123 System.out.println(ChronoUnit.DAYS.between(date1, date2)); 124 System.out.println(ChronoUnit.YEARS.between(date1, date2)); 125 126 date1 = LocalDate.of(2012, 2, 20); 127 date2 = LocalDate.of(2014, 2, 19); 128 System.out.println(ChronoUnit.YEARS.between(date1, date2)); 129 date2 = LocalDate.of(2014, 2, 20); 130 System.out.println(ChronoUnit.YEARS.between(date1, date2)); 131 date2 = LocalDate.of(2014, 2, 21); 132 System.out.println(ChronoUnit.YEARS.between(date1, date2)); 133 date2 = LocalDate.of(2010, 2, 19); 134 System.out.println(ChronoUnit.YEARS.between(date1, date2)); 135 date2 = LocalDate.of(2010, 2, 20); 136 System.out.println(ChronoUnit.YEARS.between(date1, date2)); 137 date2 = LocalDate.of(2010, 2, 21); 138 System.out.println(ChronoUnit.YEARS.between(date1, date2)); 139 140 LocalDate date3 = LocalDate.now().plus(3, ChronoUnit.DAYS); 141 System.out.println("3 days later " + date3); 142 } 143 print1()144 private static void print1() { 145 DateTimeFormatter f = new DateTimeFormatterBuilder().appendText(ChronoField.AMPM_OF_DAY) 146 .appendLiteral(' ').appendValue(ChronoField.AMPM_OF_DAY).toFormatter(); 147 System.out.println(f.format(LocalTime.of(12, 30))); 148 System.out.println(f.format(ZonedDateTime.now())); 149 } 150 print2()151 private static void print2() { 152 DateTimeFormatter f = new DateTimeFormatterBuilder().appendText(ChronoField.MONTH_OF_YEAR) 153 .appendLiteral(' ').appendValue(ChronoField.YEAR).toFormatter(); 154 System.out.println(f.format(LocalDate.now())); 155 System.out.println(f.format(YearMonth.now())); 156 System.out.println(f.format(ZonedDateTime.now())); 157 } 158 159 } 160