Java Day of Week getLastDayOfWeek(String str, int week)

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

Description

get Last Day Of Week

License

Open Source License

Declaration


public static String getLastDayOfWeek(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 getLastDayOfWeek(String str, int week) throws ParseException {
        String conStr = null;//from   w ww  .  j a  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 = 8 - dayOfWeek;
        else
            gap = 0;

        return addDays(conStr, gap);
    }

    public static String getLastDayOfWeek(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 = 8 - dayOfWeek;
        else
            gap = 0;

        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. getLastDayOfWeek(Date date)
  2. getLastDayOfWeek(Date date)
  3. getLastDayOfWeek(Date date)
  4. getLastDayOfWeek(Date date)
  5. getLastDayOfWeek(Date date)
  6. getLastDayWeek(String s)
  7. getLastMondayOfWeek(Date date)
  8. getLastWeekDate(Date date)
  9. getLastWeekDay(int weekDay, Date end)