Android Day of Week Get getLastDayOfWeek(String format)

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

Description

get Last Day Of Week

License

Apache License

Declaration

public static String getLastDayOfWeek(String format) 

Method Source Code

//package com.java2s;
/*/*from   ww w  .j a v  a 2 s .  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 {

    public static String getLastDayOfWeek(String format) {
        return getDayOfWeek(format, Calendar.SUNDAY);
    }

    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(String format, int calendarField)
  2. getDayOfWeek(String format, int calendarField)
  3. getFirstDayOfWeek(String format)
  4. getFirstDayOfWeek(String format)
  5. getLastDayOfWeek(String format)
  6. getCurrentDayOfWeek()
  7. getFirstDayOfWeek(Context context)
  8. getFirstDayOfWeekAsCalendar(Context context)