Java Weekend getWeekRange(String strBeginDate, String strEndDate, String pattern)

Here you can find the source of getWeekRange(String strBeginDate, String strEndDate, String pattern)

Description

get Week Range

License

Open Source License

Declaration

public static List<String> getWeekRange(String strBeginDate, String strEndDate, String pattern) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from  www .  j  a  v a  2s.  c  o m*/
     * default date format pattern
     */
    public final static String DATE_FORMAT = "yyyy-MM-dd";

    public static List<String> getWeekRange(String strBeginDate, String strEndDate, String pattern) {
        List<String> ret = new ArrayList<String>();
        Date beginDate = parseDate(strBeginDate);
        Date endDate = parseDate(strEndDate);

        String beginFormat = formatDate(beginDate, pattern);
        String endFormat = formatDate(endDate, pattern);
        ret.add(beginFormat);

        while (!beginFormat.equals(endFormat)) {
            beginDate = addWeek(beginDate, 1);
            beginFormat = formatDate(beginDate, pattern);
            ret.add(beginFormat);
        }
        return ret;
    }

    /**
     * parse date with the default pattern
     *
     * @param date string date
     * @return the parsed date
     */
    public static Date parseDate(String date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            return new Date();
        }
    }

    public static Date parseDate(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            return new Date();
        }
    }

    /**
     * format date with the default pattern
     *
     * @param date the date that want to format to string
     * @return the formated date
     */
    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
        return format.format(date);
    }

    /**
     * format date with the given pattern
     *
     * @param date the date that want to format to string
     * @param pattern the formated pattern
     * @return the formated date
     */
    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static Date addWeek(Date date, int i) {
        Calendar calendar = getDefaultCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.WEEK_OF_YEAR, i);
        return calendar.getTime();
    }

    /**
     * get the default calendar
     *
     * @return the calendar instance
     */
    public static Calendar getDefaultCalendar() {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        return calendar;
    }
}

Related

  1. endOfWeek(Date date, String format)
  2. getCalendarWeekOfDate(Date date)
  3. getCurWeekEnd(String strDate)
  4. isEndOfWeek()
  5. isWeekend(Calendar calendar)
  6. isWeekend(Calendar dateCal)
  7. isWeekend(Date date)