Java examples for java.util:Day
Computes the eastern sunday for the given year.
/**/*from w w w.j a v a 2s . c o m*/ * The MIT License * * Copyright (C) 2007 Asterios Raptis * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Main{ public static void main(String[] argv) throws Exception{ int year = 2; System.out.println(computeEasternSunday(year)); } /** * Computes the eastern sunday for the given year. Year should be greater than 1583. * * @param year * The year to compute the eastern sunday. * @return The eastern sunday. */ public static Date computeEasternSunday(final int year) { final int easternSundayNumber = computeEasternSundayNumber(year); final int month = easternSundayNumber / 31; final int day = easternSundayNumber % 31 + 1; return CreateDateUtils.newDate(year, month - 1, day); } /** * Computes the number from eastern sunday for the given year. Year should be greater the 1583. * * @param year * The year to compute the number from eastern sunday. * @return The number from eastern sunday. */ public static int computeEasternSundayNumber(final int year) { final int i = year % 19; final int j = year / 100; final int k = year % 100; final int l = (19 * i + j - j / 4 - (j - (j + 8) / 25 + 1) / 3 + 15) % 30; final int m = (32 + 2 * (j % 4) + 2 * (k / 4) - l - k % 4) % 7; final int n = l + m - 7 * ((i + 11 * l + 22 * m) / 451) + 114; return n; } }