Java Week Day getWeekDay(String dateString, int weekDay)

Here you can find the source of getWeekDay(String dateString, int weekDay)

Description

return date's weekday value of specified string value in format: yyyy-MM-dd Date first = DateUtil.getMonday(today,Calendar.SUNDAY); Date last = DateUtil.getMonday(today,Calendar.SATURDAY);

License

Apache License

Parameter

Parameter Description
dateString String value of date
weekDay int index of weekday to get, first Calendar.SUNDAY, last Calendar.SATURDAY

Return

Date value

Declaration

public static Date getWeekDay(String dateString, int weekDay) 

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 {
    /**/*from  w w w . j ava2 s .  co m*/
     * return date's weekday value of specified string value in format: yyyy-MM-dd
     * Date first = DateUtil.getMonday(today,Calendar.SUNDAY);
     * Date last = DateUtil.getMonday(today,Calendar.SATURDAY);
     *
     * @param dateString String value of date
     * @param weekDay    int index of weekday to get, first Calendar.SUNDAY, last Calendar.SATURDAY
     * @return Date value
     */
    public static Date getWeekDay(String dateString, int weekDay) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        if (date != null) {
            calendar.setTime(date);
        }
        // DAY_OF_WEEK
        // Field number for get and set indicating the day of the week. This field takes values
        // SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY
        calendar.set(Calendar.DAY_OF_WEEK, weekDay);
        calendar.add(Calendar.DATE, 1);
        return calendar.getTime();
    }
}

Related

  1. getWeekDay(Date date)
  2. getWeekDay(Date dt, int weekDay)
  3. getWeekDay(int weekday)
  4. getWeekDay(String date)
  5. getWeekday(String date)
  6. getWeekDay(String str)
  7. getWeekDayByDate(Date date)
  8. getWeekDayCN()
  9. getWeekdayForInt(int value)