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.Month.DECEMBER; 35 import static org.threeten.bp.temporal.ChronoField.DAY_OF_MONTH; 36 import static org.threeten.bp.temporal.ChronoField.DAY_OF_YEAR; 37 import static org.threeten.bp.temporal.ChronoField.MONTH_OF_YEAR; 38 import static org.threeten.bp.temporal.ChronoField.YEAR; 39 import static org.threeten.bp.temporal.TemporalAdjusters.lastDayOfMonth; 40 41 import java.util.Locale; 42 43 import org.threeten.bp.format.DateTimeFormatter; 44 import org.threeten.bp.format.DateTimeFormatterBuilder; 45 import org.threeten.bp.format.SignStyle; 46 47 /** 48 * Examples for this project. 49 */ 50 public class Examples { 51 52 /** 53 * Main method. 54 * @param args no arguments needed 55 */ main(String[] args)56 public static void main(String[] args) { 57 Clock clock = Clock.systemDefaultZone(); 58 59 ZonedDateTime zdt = ZonedDateTime.now(clock); 60 System.out.println("Current date-time: " + zdt); 61 62 ZonedDateTime zdtNewYork = ZonedDateTime.now(Clock.system(ZoneId.of("America/New_York"))); 63 System.out.println("Current date-time in New York: " + zdtNewYork); 64 65 ZonedDateTime zdtParis = ZonedDateTime.now(Clock.system(ZoneId.of("Europe/Paris"))); 66 System.out.println("Current date-time in Paris: " + zdtParis); 67 68 LocalDateTime ldt = LocalDateTime.now(clock); 69 System.out.println("Current local date-time: " + ldt); 70 71 Year year = Year.now(clock); 72 System.out.println("Year: " + year.getValue()); 73 74 LocalDate today = LocalDate.now(clock); 75 System.out.println("Today: " + today); 76 77 System.out.println("Current day-of-year: " + today.get(DAY_OF_YEAR)); 78 79 LocalTime time = LocalTime.now(clock); 80 System.out.println("Current time of day: " + time); 81 82 LocalDate later = LocalDate.now(clock).plusMonths(2).plusDays(3); 83 System.out.println("Two months three days after today: " + later); 84 85 // ISOPeriod period = ISOPeriod.of(3, MONTHS); 86 // LocalDate moreLater = LocalDate.now(clock).plus(period); 87 // System.out.println("Period " + period + " after today : " + moreLater); 88 89 LocalDate dec = LocalDate.now(clock).with(DECEMBER); 90 System.out.println("Change to same day in December: " + dec); 91 92 LocalDate lastDayOfMonth = LocalDate.now(clock).with(lastDayOfMonth()); 93 System.out.println("Last day of month: " + lastDayOfMonth); 94 95 /// LocalDate tempDate = LocalDate.now(clock); 96 // DateTimeFields fri13matcher = DateTimeFields.of( 97 // DAY_OF_WEEK, FRIDAY.getValue(), DAY_OF_MONTH, 13); 98 // boolean fri13 = fri13matcher.matches(tempDate); 99 // System.out.println("Is Friday the Thirteenth: " + fri13); 100 101 LocalDateTime dt = LocalDateTime.of(2008, 3, 30, 1, 30); 102 System.out.println("Local date-time in Spring DST gap: " + dt); 103 104 ZonedDateTime resolved = ZonedDateTime.of(dt, ZoneId.of("Europe/London")); 105 System.out.println("...resolved to valid date-time in Europe/London: " + resolved); 106 107 String formattedRFC = DateTimeFormatter.RFC_1123_DATE_TIME.format(resolved); 108 System.out.println("...printed as RFC1123: " + formattedRFC); 109 110 DateTimeFormatter f = new DateTimeFormatterBuilder() 111 .appendValue(YEAR, 4, 10, SignStyle.ALWAYS) 112 .appendLiteral(' ') 113 .appendText(MONTH_OF_YEAR) 114 .appendLiteral('(') 115 .appendValue(MONTH_OF_YEAR) 116 .appendLiteral(')') 117 .appendLiteral(' ') 118 .appendValue(DAY_OF_MONTH, 2) 119 .toFormatter(Locale.ENGLISH); 120 String formatted = f.format(resolved); 121 System.out.println("...printed using complex format: " + formatted); 122 123 MonthDay bday = MonthDay.of(DECEMBER, 3); 124 System.out.println("Brazillian birthday (no year): " + bday); 125 } 126 127 } 128