Java Day getDayInWeekBeginAndEnd(Date date)

Here you can find the source of getDayInWeekBeginAndEnd(Date date)

Description

get Day In Week Begin And End

License

Apache License

Declaration

public static String[] getDayInWeekBeginAndEnd(Date date) 

Method Source Code

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

import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {

    public static String[] getDayInWeekBeginAndEnd(Date date) {
        java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.setTime(date);//from  w w  w. j  ava2 s  .  com
        int week = cal.get(java.util.Calendar.DAY_OF_WEEK) - 1;
        String begin, end;
        if (week == 0) {
            begin = addDate(date, -6);
            end = format(date);
        } else {
            int days = 7 - week;
            begin = addDate(date, -(week - 1));
            end = addDate(date, days);
        }
        return new String[] { begin, end };
    }

    public static String addDate(Date date, int k) {
        java.util.Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, k);
        return format(cal.getTime());
    }

    public static String format(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sf = new SimpleDateFormat(pattern);
        return sf.format(date);
    }

    public static String format(Date date) {
        return format(date, "yyyy-MM-dd");
    }
}

Related

  1. getDayDesc(Date arg)
  2. getDayEnd(Date date)
  3. getDayEndTime(int off, String timezone)
  4. getDayForDate(String dateToCheck, String pattern)
  5. getDayFormat(Date date)
  6. getDayInWeekForMonthIn2Characters(int year, int month)
  7. getDayName(String dateString)
  8. getDayNameForDate(java.util.Date dt, boolean fullname)
  9. getDayNames(Locale locale)