Java SQL Date getEndWeekDayOfMonth(String year, String month)

Here you can find the source of getEndWeekDayOfMonth(String year, String month)

Description

get End Week Day Of Month

License

Apache License

Declaration

public static List getEndWeekDayOfMonth(String year, String month) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

public class Main {
    /** The Constant MONTH. */
    public final static String[] MONTH = { "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };

    public static List getEndWeekDayOfMonth(String year, String month) {
        List list = new ArrayList();
        String date = "";
        int days = daysInMonth(year, month);
        int weekday = 0;
        for (int i = 1; i <= days; i++) {
            weekday = getWeekOfMonth(year, month, String.valueOf(i));
            if (weekday == 5) {
                if (i < 10) {
                    list.add(year + month + "0" + String.valueOf(i));
                } else {
                    list.add(year + month + String.valueOf(i));
                }//from  w  w w  .j  a v  a  2  s . co m
            }
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println("end week list[" + i + "]:" + list.get(i));
        }

        return list;
    }

    public static int daysInMonth(String argYear, String argMonth) {
        int year = Integer.parseInt(argYear);
        int month = Integer.parseInt(argMonth);

        GregorianCalendar c = new GregorianCalendar(year, month, 0);
        int[] daysInMonths = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        daysInMonths[1] += c.isLeapYear(c.get(GregorianCalendar.YEAR)) ? 1 : 0;
        return daysInMonths[c.get(GregorianCalendar.MONTH)];
    }

    public static int getWeekOfMonth(String year, String month, String day) {
        java.sql.Date myDate = getDate(year, month, day);
        int index = 0;
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(myDate);
            index = cal.get(Calendar.DAY_OF_WEEK);
            if (index <= 1) {
                index = 7;
            } else {
                index = index - 1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return index;

    }

    /**
     * Adds the specified (signed) amount of time to the given time field, based
     * on the calendar's rules.
     * 
     * @param date
     *            the date
     * @param field
     *            the field
     * @param amount
     *            the amount
     * @return the date
     */
    public static Date add(Date date, int field, long amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, (int) amount);
        return calendar.getTime();
    }

    public static java.sql.Date getDate(String year, String month, String day) {
        java.sql.Date result = null;
        try {
            String str = year + "-" + month + "-" + day;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            java.util.Date date1 = dateFormat.parse(str);
            result = new java.sql.Date(date1.getTime());
        } catch (Exception e) {
            System.out.println("Exception " + e);
        }
        return result;
    }
}

Related

  1. getClasses()
  2. getCurrDay()
  3. getCurrentYear()
  4. getDayOfPerMonth(String theDataStr)
  5. getDiffDays(String begin_dt, String end_dt)
  6. getFileName()
  7. getFirstDayOfNextMonth()
  8. getFirstDayOfThisMonth()
  9. getInsertarEmpleado()