Here you can find the source of getWeekDay(Date date)
Parameter | Description |
---|---|
date | specified date |
public static int getWeekDay(Date date)
//package com.java2s; /*// w w w .ja v a 2 s. co m * File: $RCSfile$ * * Copyright (c) 2005 Wincor Nixdorf International GmbH, * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany * All Rights Reserved. * * This software is the confidential and proprietary information * of Wincor Nixdorf ("Confidential Information"). You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered * into with Wincor Nixdorf. */ import java.util.Date; import java.util.Calendar; import java.util.List; import java.util.ArrayList; public class Main { /** * Return week day of specified date * * @param date specified date * @return week day of @param date */ public static int getWeekDay(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return c.get(Calendar.DAY_OF_WEEK); } /** * Return dates list with specified week day between specified begin data and end date * * @param weekDay week day for exampke, Mon, Tus..... * @param beginDate begin date * @param endDate end date * @return array includes expected date objects whose week day equals to @param weekDay */ public static List getWeekDay(int weekDay, Date beginDate, Date endDate) { int startMonth = getDateMonth(beginDate).intValue(); int startYear = getDateYear(beginDate).intValue(); Date startDateMonthFirstDate = getFirstDayOfMonth(startYear, startMonth - 1); int firstDayOfStartDate = getWeekDay(startDateMonthFirstDate) - 1; int offset = weekDay - firstDayOfStartDate; if (offset < 0) { offset += 7; } Date weekStartDate = add(startDateMonthFirstDate, offset, Calendar.DAY_OF_YEAR); List lstResult = new ArrayList(); while (compareYMD(weekStartDate, endDate)) { lstResult.add(weekStartDate); weekStartDate = add(weekStartDate, 7, Calendar.DAY_OF_YEAR); } return lstResult; } /** * Return month of specified date * * @param date specified date * @return month value */ public static Integer getDateMonth(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return new Integer(c.get(Calendar.MONTH) + 1); } /** * Return year of specified date * * @param date specified date * @return year value */ public static Integer getDateYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); return new Integer(c.get(Calendar.YEAR)); } /** * Return first day of date with specified year and month * * @param year specified year * @param month specified month * @return first day value of year of current date */ public static Date getFirstDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month); cal.set(Calendar.DAY_OF_MONTH, 1); return cal.getTime(); } /** * Add Interval to Date * * @param inputDate Date to Add * @param interval Interval to added. Subtract is interval is nagative * @param calendarUnit Calendar.Year/Calendar.Month/Calendar.Day/Calendar.Hour/Calendar.MInute * @return Date added */ public static Date add(Date inputDate, int interval, int calendarUnit) { Calendar cal = Calendar.getInstance(); cal.setTime(formateDateWithoutTime(inputDate)); cal.add(calendarUnit, interval); return cal.getTime(); } /** * add by xie compare date only compare year month day * * @param begindate begin date * @param enddate end date * @return begindate > endate as false */ public static boolean compareYMD(Date begindate, Date enddate) { boolean isBefore = true; Calendar startCalendar = Calendar.getInstance(); startCalendar.setTime(begindate); Calendar endCalendar = Calendar.getInstance(); endCalendar.setTime(enddate); int startYear = startCalendar.get(Calendar.YEAR); int startMonth = startCalendar.get(Calendar.MONTH); int startDay = startCalendar.get(Calendar.DAY_OF_MONTH); int endYear = endCalendar.get(Calendar.YEAR); int endMonth = endCalendar.get(Calendar.MONTH); int endDay = endCalendar.get(Calendar.DAY_OF_MONTH); if (startYear > endYear) { isBefore = false; } else if (startYear == endYear) { if (startMonth > endMonth) { isBefore = false; } else if (startMonth == endMonth) { if (startDay > endDay) { isBefore = false; } } } return isBefore; } /** * Format Date to make it''s hour/minute/second to 0 * * @param inputDate Date to Format * @return Date with Year/Month/Day is same with input one while hour/minute/second is 0 */ public static Date formateDateWithoutTime(Date inputDate) { Date outputDate = inputDate; Calendar cal = Calendar.getInstance(); cal.setTime(outputDate); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); outputDate = cal.getTime(); return outputDate; } }