Java Year Format getStartAndEndWeekOfMonth(int year, int month, int week, String format)

Here you can find the source of getStartAndEndWeekOfMonth(int year, int month, int week, String format)

Description

get Start And End Week Of Month

License

Open Source License

Declaration

public static String[] getStartAndEndWeekOfMonth(int year, int month, int week, String format) 

Method Source Code


//package com.java2s;

import java.util.*;
import java.text.*;

public class Main {
    public static String[] getStartAndEndWeekOfMonth(int year, int month, int week, String format) {

        SimpleDateFormat formater = new SimpleDateFormat(format);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, 0);

        int lastWeek = calendar.get(Calendar.WEEK_OF_MONTH);
        if (week > lastWeek)
            return null;

        String lastDate = formater.format(calendar.getTime());

        String[] result = new String[2];
        calendar.set(Calendar.WEEK_OF_MONTH, week);
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        if (week == 1)
            calendar.set(Calendar.DATE, 1);

        result[0] = formater.format(calendar.getTime());

        if (lastWeek == week) {
            result[1] = lastDate;//ww  w.j  a v a 2  s .  c o  m
        } else {
            calendar.set(Calendar.DAY_OF_WEEK, 7);
            result[1] = formater.format(calendar.getTime());
        }
        return result;
    }

    public static Date getTime(String hhmm) {
        if (hhmm != null && hhmm.length() == 4 && isDigit(hhmm)) {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hhmm.substring(0, 2)));
            cal.set(Calendar.MINUTE, Integer.parseInt(hhmm.substring(2, 4)));

            return cal.getTime();
        }

        return null;
    }

    private static boolean isDigit(String digitStr) {
        if (digitStr != null) {
            for (int i = 0; i < digitStr.length(); i++)
                if (!Character.isDigit(digitStr.charAt(i)))
                    return false;
        }
        return true;
    }
}

Related

  1. get_Oracle_DateString_YYYYMMdd_HHmmss(java.util.Date date)
  2. getDDMMYYY_HHMM()
  3. getDDMMYYYYDate(Date date)
  4. getFirstDate(String yearMonthStr, String yearMonthFormat, String dateFormat)
  5. getFormatToDateSimple(String yyyyMMdd)
  6. getStringFormatMMYYWithoutHyphens(Date aDate)
  7. getStringFormatYYYYMMddWithHyphens(Date aDate)
  8. getTimeByYMD()
  9. getYear(SimpleDateFormat df, String dateStr)