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