Android Day of Week Get 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;
/*//from  ww  w.j  a 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. getDayOfTheWeek(Date date)
  2. getDayOfWeekZeroIndexed(Date dateTime)
  3. getReadableWeekDay(int day)
  4. getStringDayOfWeek(Date date)
  5. getDayOfWeek(String format, int calendarField)
  6. getFirstDayOfWeek(String format)
  7. getFirstDayOfWeek(String format)
  8. getLastDayOfWeek(String format)
  9. getLastDayOfWeek(String format)