Java Day of Week getDayOfWeek(String format, int calendarField)

Here you can find the source of getDayOfWeek(String format, int calendarField)

Description

get Day Of Week

License

Apache License

Declaration

private static String getDayOfWeek(String format, int calendarField) 

Method Source Code

//package com.java2s;
/*/*  w  ww  .  ja v  a2s.  c  o  m*/
 * Copyright (C) 2013 www.418log.org
 * 
 * Licensed 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.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {

    private static String getDayOfWeek(String format, int calendarField) {
        String strDate = null;
        try {
            Calendar c = new GregorianCalendar();
            SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
            int week = c.get(Calendar.DAY_OF_WEEK);
            if (week == calendarField) {
                strDate = mSimpleDateFormat.format(c.getTime());
            } else {
                int offectDay = calendarField - week;
                if (calendarField == Calendar.SUNDAY) {
                    offectDay = 7 - Math.abs(offectDay);
                }
                c.add(Calendar.DATE, offectDay);
                strDate = mSimpleDateFormat.format(c.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return strDate;
    }
}

Related

  1. getDayOfWeek(int year, int month, int day)
  2. getDayOfWeek(int year, int month, int day)
  3. getDayOfWeek(String date, String pattern)
  4. getDayOfWeek(String datetime)
  5. getDayOfWeek(String f)
  6. getDayOfWeekAsString(String sYear, String sMonth, String sDay)
  7. getDayofWeekByDay(String year, String month, String day)
  8. getDayofWeekForContainedDate(Date dt, DayOfWeek dow)
  9. getDaysByWeek(Integer month, Integer year, Integer week)