Java Day of Week getWeekDate(String date)

Here you can find the source of getWeekDate(String date)

Description

get Week Date

License

Open Source License

Declaration

public static String[] getWeekDate(String date) 

Method Source Code


//package com.java2s;
import java.util.Calendar;

public class Main {
    public static String[] getWeekDate(String date) {
        String[] weekDays = new String[7];
        weekDays[0] = getMondayDate(date);
        weekDays[1] = getWeekDays(weekDays[0], 1);
        weekDays[2] = getWeekDays(weekDays[0], 2);
        weekDays[3] = getWeekDays(weekDays[0], 3);
        weekDays[4] = getWeekDays(weekDays[0], 4);
        weekDays[5] = getWeekDays(weekDays[0], 5);
        weekDays[6] = getWeekDays(weekDays[0], 6);

        return weekDays;
    }/*from  w w w  .  ja  va2 s. c o  m*/

    public static String getMondayDate(String date) {
        String startDate;

        int year = getYear(date);
        int month = getMonth(date);
        int day = getDate(date);

        int curDayOfWeek = getDayOfWeek(year, month, day);

        int startTmp = 2 - curDayOfWeek;

        if (startTmp == 0)
            return date;

        if (startTmp > 0) {
            startTmp = startTmp - 7;
        }

        Calendar tmpCalendar = Calendar.getInstance();

        tmpCalendar.set(year, month - 1, day);

        tmpCalendar.add(Calendar.DATE, startTmp);

        startDate = Integer.toString(tmpCalendar.get(Calendar.YEAR));

        if ((tmpCalendar.get(Calendar.MONTH) + 1) < 10)
            startDate += "0";

        startDate += Integer.toString((tmpCalendar.get(Calendar.MONTH)) + 1);

        if (tmpCalendar.get(Calendar.DATE) < 10)
            startDate += "0";

        startDate += Integer.toString(tmpCalendar.get(Calendar.DATE));

        return startDate;
    }

    public static String getWeekDays(String _date, int _amount) {
        String res;

        int year = Integer.parseInt(_date.substring(0, 4));
        int month = Integer.parseInt(_date.substring(4, 6));
        int day = Integer.parseInt(_date.substring(6, 8));

        Calendar wCal = Calendar.getInstance();
        wCal.set(year, month - 1, day);
        wCal.add(Calendar.DATE, _amount);

        year = wCal.get(Calendar.YEAR);
        month = wCal.get(Calendar.MONTH) + 1;
        day = wCal.get(Calendar.DATE);

        res = Integer.toString(year);

        if (month < 10)
            res += "0" + Integer.toString(month);
        else
            res += Integer.toString(month);

        if (day < 10)
            res += "0";

        res += Integer.toString(day);

        return res;
    }

    public static int getYear(String date) {
        return Integer.parseInt(date.substring(0, 4));
    }

    public static int getMonth(String date) {
        return Integer.parseInt(date.substring(4, 6));
    }

    public static int getDate(String date) {
        return Integer.parseInt(date.substring(6, 8));
    }

    public static int getDayOfWeek(int _year, int _month, int _day) {
        int year, month, date;

        Calendar cal = Calendar.getInstance();
        cal.set(_year, _month - 1, _day);

        return cal.get(Calendar.DAY_OF_WEEK);
    }
}

Related

  1. getWeek(java.util.Date date)
  2. getWeek(java.util.Date today)
  3. getWeekBegin(Date date)
  4. getWeekCategory(final Date date)
  5. getWeekChineseIndex(Date curDate)
  6. getWeekDateList(Date today, int index)
  7. getWeekday(Date d)
  8. getWeekDay(Date d)
  9. getWeekDay(Date date)