Java Week Day getDayOfWeek(Date date, int day)

Here you can find the source of getDayOfWeek(Date date, int day)

Description

get Day Of Week

License

Apache License

Declaration

public static Date getDayOfWeek(Date date, int day) 

Method Source Code

//package com.java2s;
/**/*from w w w. ja  va  2 s  .  c o m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

public class Main {

    public static Date getDayOfWeek(Date date, int day) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + day - 1);
        return c.getTime();
    }

    public static int getDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (dayOfWeek < 0)
            dayOfWeek = 0;
        return dayOfWeek;
    }

    public static Date getFirstDayOfWeek(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
        return c.getTime();
    }
}

Related

  1. getDayOfWeek(Date date)
  2. getDayOfWeek(Date date)
  3. getDayOfWeek(Date date)
  4. getDayOfWeek(Date date)
  5. getDayOfWeek(Date date)
  6. getDayOfWeek(Date dt)
  7. getDayOfWeek(Date start, int day)
  8. getDayOfWeek(int _year, int _month, int _day)
  9. getDayOfWeek(int year, int month, int day)