Android Day of Week Get getFirstDayOfWeek(String format)

Here you can find the source of getFirstDayOfWeek(String format)

Description

get First Day Of Week

Declaration

public static String getFirstDayOfWeek(String format) 

Method Source Code

//package com.java2s;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import java.util.GregorianCalendar;

public class Main {

    public static String getFirstDayOfWeek(String format) {
        return getDayOfWeek(format, Calendar.MONDAY);
    }/*from  w  w  w  .jav a2 s  .  c o m*/

    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. getDayOfWeekZeroIndexed(Date dateTime)
  2. getReadableWeekDay(int day)
  3. getStringDayOfWeek(Date date)
  4. getDayOfWeek(String format, int calendarField)
  5. getDayOfWeek(String format, int calendarField)
  6. getFirstDayOfWeek(String format)
  7. getLastDayOfWeek(String format)
  8. getLastDayOfWeek(String format)
  9. getCurrentDayOfWeek()