Mon, 09 Nov 2015 13:45:10 -0800
Merge
sundar@1524 | 1 | # compute Mothers day of the given the year |
sundar@1524 | 2 | |
sundar@1524 | 3 | /* |
sundar@1524 | 4 | * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
sundar@1524 | 5 | * |
sundar@1524 | 6 | * Redistribution and use in source and binary forms, with or without |
sundar@1524 | 7 | * modification, are permitted provided that the following conditions |
sundar@1524 | 8 | * are met: |
sundar@1524 | 9 | * |
sundar@1524 | 10 | * - Redistributions of source code must retain the above copyright |
sundar@1524 | 11 | * notice, this list of conditions and the following disclaimer. |
sundar@1524 | 12 | * |
sundar@1524 | 13 | * - Redistributions in binary form must reproduce the above copyright |
sundar@1524 | 14 | * notice, this list of conditions and the following disclaimer in the |
sundar@1524 | 15 | * documentation and/or other materials provided with the distribution. |
sundar@1524 | 16 | * |
sundar@1524 | 17 | * - Neither the name of Oracle nor the names of its |
sundar@1524 | 18 | * contributors may be used to endorse or promote products derived |
sundar@1524 | 19 | * from this software without specific prior written permission. |
sundar@1524 | 20 | * |
sundar@1524 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
sundar@1524 | 22 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
sundar@1524 | 23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
sundar@1524 | 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
sundar@1524 | 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
sundar@1524 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
sundar@1524 | 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
sundar@1524 | 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
sundar@1524 | 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
sundar@1524 | 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
sundar@1524 | 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
sundar@1524 | 32 | */ |
sundar@1524 | 33 | |
sundar@1524 | 34 | // print "Mother's day" of the given year using Java Time API |
sundar@1524 | 35 | |
sundar@1524 | 36 | if (arguments.length == 0) { |
sundar@1524 | 37 | print("Usage: jjs mothers_day.js -- year"); |
sundar@1524 | 38 | exit(1); |
sundar@1524 | 39 | } |
sundar@1524 | 40 | |
sundar@1524 | 41 | // java classes used |
sundar@1524 | 42 | var DayOfWeek = java.time.DayOfWeek; |
sundar@1524 | 43 | var LocalDate = java.time.LocalDate; |
sundar@1524 | 44 | var TemporalAdjusters = java.time.temporal.TemporalAdjusters; |
sundar@1524 | 45 | |
sundar@1524 | 46 | var year = parseInt(arguments[0]); |
sundar@1524 | 47 | |
sundar@1524 | 48 | // See: https://en.wikipedia.org/?title=Mother%27s_Day |
sundar@1524 | 49 | // We need second Sunday of May. Make April 30 of the given |
sundar@1524 | 50 | // year adjust and adjust to next Sunday from there twice. To adjust a Date |
sundar@1524 | 51 | // we use a common TemporalAdjuster provided in JDK8. |
sundar@1524 | 52 | // https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalAdjusters.html |
sundar@1524 | 53 | |
sundar@1524 | 54 | print(LocalDate.of(year, 4, 30). |
sundar@1524 | 55 | with(TemporalAdjusters.next(DayOfWeek.SUNDAY)). |
sundar@1524 | 56 | with(TemporalAdjusters.next(DayOfWeek.SUNDAY))); |