Java Week Day getFirstDayOfWeek(String str, int week)

Here you can find the source of getFirstDayOfWeek(String str, int week)

Description

get First Day Of Week

License

Open Source License

Declaration

public static String getFirstDayOfWeek(String str, int week) throws ParseException 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {

    public static String getFirstDayOfWeek(String str, int week) throws ParseException {
        String conStr = null;//from   w  ww.  ja va2  s  . c  o  m
        int dayOfWeek = 0;

        if (week == 0) {
            conStr = str;
            dayOfWeek = getCalendar(conStr).get(Calendar.DAY_OF_WEEK);
        } else {
            conStr = addDays(str, week * 7);
            dayOfWeek = getCalendar(conStr).get(Calendar.DAY_OF_WEEK);
        }

        int gap = 0;
        if (dayOfWeek != 1)
            gap = dayOfWeek - 2;
        else
            gap = 6;

        return addDays(conStr, -gap);
    }

    public static String getFirstDayOfWeek(Date date, int week) throws ParseException {
        Date conDate = null;
        int dayOfWeek = 0;

        if (week == 0) {
            conDate = date;
            dayOfWeek = getCalendar(conDate).get(Calendar.DAY_OF_WEEK);
        } else {
            conDate = addDays2Date(date, week * 7);
            dayOfWeek = getCalendar(conDate).get(Calendar.DAY_OF_WEEK);
        }

        int gap = 0;
        if (dayOfWeek != 1)
            gap = dayOfWeek - 2;
        else
            gap = 6;

        return addDays(conDate, -gap);
    }

    public static Calendar getCalendar(String str) {
        int yy = Integer.parseInt(str.substring(0, 4));
        int mm = Integer.parseInt(str.substring(4, 6)) - 1;
        int dd = Integer.parseInt(str.substring(6, 8));

        Calendar cal = Calendar.getInstance();
        cal.set(yy, mm, dd);
        return cal;
    }

    public static Calendar getCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }

    public static String addDays(String str, int days) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        Date date = fmt.parse(str);
        date.setTime(date.getTime() + (long) days * 1000L * 60L * 60L * 24L);
        return fmt.format(date);
    }

    public static String addDays(Date date, int days) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        date.setTime(date.getTime() + (long) days * 1000L * 60L * 60L * 24L);
        return fmt.format(date);
    }

    public static Date addDays2Date(String str, int days) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        Date date = fmt.parse(str);
        date.setTime(date.getTime() + (long) days * 1000L * 60L * 60L * 24L);
        return date;
    }

    public static Date addDays2Date(Date date, int days) throws ParseException {
        date.setTime(date.getTime() + (long) days * 1000L * 60L * 60L * 24L);
        return date;
    }
}

Related

  1. getFirstDayOfWeek()
  2. getFirstDayOfWeek(Date date)
  3. getFirstDayOfWeek(Date date)
  4. getFirstDayOfWeek(final Date date)
  5. getFirstDayOfWeek(String format)
  6. getFirstDayOfWeek(String week)
  7. getFirstDayOfWeek(String week)
  8. getFriday(Date date)
  9. getLastDayOfCurrWeek()