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

Declaration

private static String getDayOfWeek(String format, int calendarField) 

Method Source Code

//package com.java2s;

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