开发者

Dates of start and end of current week in Android

I need to find start and end day of current week in Android.

examples

today: Oct 12 2011 -> result: Oct 10 2011 - Oct 16 2011

today: Oct 1 2001 -> result: Sep 26 2011 - Oct 2 2011

today: Dec 30 2011 -> result: Dec 24 2001 - Jan 1 2011

Using c.get(Calendar.WEEK_OF_YEAR); I can开发者_如何转开发 get the week number but how to get the start & end date? I've found an answer here pointing to MonthDisplayHelper , but how to use it?

Thanks!


Used this sintax and it worked

    Calendar c1 = Calendar.getInstance();

    //first day of week
    c1.set(Calendar.DAY_OF_WEEK, 1);

    int year1 = c1.get(Calendar.YEAR);
    int month1 = c1.get(Calendar.MONTH)+1;
    int day1 = c1.get(Calendar.DAY_OF_MONTH);

    //last day of week
    c1.set(Calendar.DAY_OF_WEEK, 7);

    int year7 = c1.get(Calendar.YEAR);
    int month7 = c1.get(Calendar.MONTH)+1;
    int day7 = c1.get(Calendar.DAY_OF_MONTH);  


Here is the good example code, which gives you the current week of the year as well as week starting and ending date, Just what you need to do is set the starting day of the week in code, In my case I set it as SUNDAY,

// get Current Week of the year
    calendar=Calendar.getInstance();
    Log.v("Current Week", String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR)));
    int current_week=calendar.get(Calendar.WEEK_OF_YEAR);
    int week_start_day=calendar.getFirstDayOfWeek(); // this will get the starting day os week in integer format i-e 1 if monday
    Toast.makeText(getContext(),"Current Week is"+current_week +"Start Day is"+week_start_day,Toast.LENGTH_SHORT).show();


    // get the starting and ending date
    // Set the calendar to sunday of the current week
    calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    System.out.println("Current week = " + Calendar.DAY_OF_WEEK);

    // Print dates of the current week starting on Sunday
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    String startDate = "", endDate = "";

    startDate = df.format(calendar.getTime());
    calendar.add(Calendar.DATE, 6);
    endDate = df.format(calendar.getTime());

    System.out.println("Start Date = " + startDate);
    System.out.println("End Date = " + endDate);


Here is the good example for your since it showed correct date you wanted : 01 - 07 Jan 8 - 15 Jan ... etc ... by pressed BackWard button & Forward button relatively.

p/s : Please edit Date format followed as you want.

public static String getLastWeek(Calendar mCalendar) {
        // Monday
        mCalendar.add(Calendar.DAY_OF_YEAR, -13);
        Date mDateMonday = mCalendar.getTime();

        // Sunday
        mCalendar.add(Calendar.DAY_OF_YEAR, 6);
        Date mDateSunday = mCalendar.getTime();

        // Date format
        String strDateFormat = "dd MMM";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

        String MONDAY = sdf.format(mDateMonday);
        String SUNDAY = sdf.format(mDateSunday);

        // Substring
        if ((MONDAY.substring(3, 6)).equals(SUNDAY.substring(3, 6))) {
            MONDAY = MONDAY.substring(0, 2);
        }

        return MONDAY + " - " + SUNDAY;
    }

public static String getCurrentWeek(Calendar mCalendar) {
        Date date = new Date();
        mCalendar.setTime(date);

        // 1 = Sunday, 2 = Monday, etc.
        int day_of_week = mCalendar.get(Calendar.DAY_OF_WEEK); 

        int monday_offset;
        if (day_of_week == 1) {
            monday_offset = -6;
        } else
            monday_offset = (2 - day_of_week); // need to minus back
        mCalendar.add(Calendar.DAY_OF_YEAR, monday_offset);

        Date mDateMonday = mCalendar.getTime();

        // return 6 the next days of current day (object cal save current day)
        mCalendar.add(Calendar.DAY_OF_YEAR, 6);
        Date mDateSunday = mCalendar.getTime();

        //Get format date
        String strDateFormat = "dd MMM";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

        String MONDAY = sdf.format(mDateMonday);
        String SUNDAY = sdf.format(mDateSunday);

        // Sub String
        if ((MONDAY.substring(3, 6)).equals(SUNDAY.substring(3, 6))) {
            MONDAY = MONDAY.substring(0, 2);
        }

        return MONDAY + " - " + SUNDAY;
    }

public static String getNextWeek(Calendar mCalendar) {
        // Monday
        mCalendar.add(Calendar.DAY_OF_YEAR, 1);
        Date mDateMonday = mCalendar.getTime();

        // Sunday
        mCalendar.add(Calendar.DAY_OF_YEAR, 6);
        Date Week_Sunday_Date = mCalendar.getTime();

        // Date format
        String strDateFormat = "dd MMM";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

        String MONDAY = sdf.format(mDateMonday);
        String SUNDAY = sdf.format(Week_Sunday_Date);

        // Sub string
        if ((MONDAY.substring(3, 6)).equals(SUNDAY.substring(3, 6))) {
            MONDAY = MONDAY.substring(0, 2);
        }

        return MONDAY + " - " + SUNDAY;
    }


Follow this code this would help you to find start date and last date of current week . its work at my side.

Calendar calendar = Calendar.getInstance();

    Date date1 = calendar.getTime();
    SimpleDateFormat checkformate = new SimpleDateFormat("MM/yyyy");
    String currentCheckdate = checkformate.format(date1);

    int weekn = calendar.get(Calendar.WEEK_OF_MONTH);
    int month = calendar.get(Calendar.MONTH);
    int year = calendar.get(Calendar.YEAR);

    calendar.clear();
    calendar.setFirstDayOfWeek(Calendar.MONDAY);
    calendar.set(Calendar.WEEK_OF_MONTH, weekn);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.YEAR, year);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    Date datef = calendar.getTime();
    Long time = calendar.getTimeInMillis() + 518400000L;
    Date dateL = new Date(time);
    String firtdate = simpleDateFormat.format(datef);
    String lastdate = simpleDateFormat.format(dateL);
    String firtdateCheck = checkformate.format(datef);
    String lastdateCheck = checkformate.format(dateL);


    if (!firtdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {
    firtdate = calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + "1";
    }

    if (!lastdateCheck.toString().equalsIgnoreCase(currentCheckdate)) {

    int ma = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    lastdate = calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + String.valueOf(ma);
    }
    endDate = lastdate.toString();
    startDate = firtdate.toString();


Here's the Kotlin version:

val calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"))
calendar.time = Date()
calendar.firstDayOfWeek = Calendar.MONDAY // Set the starting day of the week
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY) // Pass whatever day you want to get inplace of `MONDAY`
val startDate = calendar.time
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY)
val endDate = calendar.time


This is for kotlin lovers!

                var startLocalDate = LocalDate.now()
                var endLocalDate = LocalDate.now()

                // Go backward to get Monday
                while (startLocalDate.dayOfWeek != DayOfWeek.MONDAY) {
                    startLocalDate = startLocalDate.minusDays(1)
                }

                // Go forward to get Sunday
                while (endLocalDate.dayOfWeek != DayOfWeek.SUNDAY) {
                    endLocalDate = endLocalDate.plusDays(1)
                }


// <Pre  Current Week Next>
// Week : MONDAY to SUNDAY
// Week : SUNDAY to SATURDAY  

    public class TimeSheetCalender {
    
        public static String DATE_FORMAT_PATTERN = "yyyy-MM-dd";
        public static String DATE_PATTERN = "yyyy-MM-dd";
        public static String DATE_FULL_PATTERN = "MM dd, YYYY";
    
        public static ArrayList<Date> getLastWeek(Calendar mCalendar) {
            // Monday
            mCalendar.add(Calendar.DAY_OF_YEAR, -13);
            Date mDateMonday = mCalendar.getTime();
    
            // Sunday
            mCalendar.add(Calendar.DAY_OF_YEAR, 6);
            Date mDateSunday = mCalendar.getTime();
    
            // Date format
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.US);
    
            String MONDAY = sdf.format(mDateMonday);
            String SUNDAY = sdf.format(mDateSunday);
    
            ArrayList<Date> dateList = getDates(MONDAY, SUNDAY);
            //return MONDAY + " - " + SUNDAY;
            return dateList;
        }
    
        //MONDAY - SUNDAY
        public static ArrayList<Date> getCurrentWeek(Calendar mCalendar) {
            Date date = new Date();
            mCalendar.setTime(date);
            // 1 = Sunday, 2 = Monday, etc.
            int day_of_week = mCalendar.get(Calendar.DAY_OF_WEEK);
    
            int monday_offset;
            if (day_of_week == 1) {
                monday_offset = -6;
            } else
                monday_offset = (2 - day_of_week); // need to minus back
            mCalendar.add(Calendar.DAY_OF_YEAR, monday_offset);
    
            Date mDateMonday = mCalendar.getTime();
    
            // return 6 the next days of current day (object cal save current day)
            mCalendar.add(Calendar.DAY_OF_YEAR, 6);
            Date mDateSunday = mCalendar.getTime();
    
            //Get format date
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.US);
    
            String MONDAY = sdf.format(mDateMonday);
            String SUNDAY = sdf.format(mDateSunday);
    
            Common.logData("TimeSheetCalender", "Date: " + MONDAY);
    
            ArrayList<Date> dateList = getDates(MONDAY, SUNDAY);
            //return MONDAY + " - " + SUNDAY;
            return dateList;
        }
    
        //SUNDAY - SATURDAY
        public static ArrayList<Date> getCurrentWeek_(Calendar mCalendar) {
            // Set the calendar to sunday of the current week
            mCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
    
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.US);
            String startDate = "", endDate = "";
    
            startDate = sdf.format(mCalendar.getTime());
            mCalendar.add(Calendar.DATE, 6);
            endDate = sdf.format(mCalendar.getTime());
    
            System.out.println("Start Date = " + startDate);
            System.out.println("End Date = " + endDate);
    
            ArrayList<Date> dateList = getDates(startDate, endDate);
    
            return dateList;
        }
    
        public static ArrayList<Date> getNextWeek(Calendar mCalendar) {
            // Monday
            mCalendar.add(Calendar.DAY_OF_YEAR, 1);
            Date mDateMonday = mCalendar.getTime();
    
            // Sunday
            mCalendar.add(Calendar.DAY_OF_YEAR, 6);
            Date Week_Sunday_Date = mCalendar.getTime();
    
            // Date format
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.US);
    
            String MONDAY = sdf.format(mDateMonday);
            String SUNDAY = sdf.format(Week_Sunday_Date);
    
            ArrayList<Date> dateList = getDates(MONDAY, SUNDAY);
            //return MONDAY + " - " + SUNDAY;
            return dateList;
        }
    
        private static ArrayList<Date> getDates(String dateString1, String dateString2) {
            ArrayList<Date> dates = new ArrayList<Date>();
            DateFormat df1 = new SimpleDateFormat(DATE_FORMAT_PATTERN, Locale.US);
    
            Date date1 = null;
            Date date2 = null;
    
            try {
                date1 = df1.parse(dateString1);
                date2 = df1.parse(dateString2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    
            Calendar cal1 = Calendar.getInstance(Locale.US);
            cal1.setTime(date1);
    
            Calendar cal2 = Calendar.getInstance(Locale.US);
            cal2.setTime(date2);
    
            while (!cal1.after(cal2)) {
                dates.add(cal1.getTime());
                cal1.add(Calendar.DATE, 1);
    
                Common.logData("TimeSheetCalender", "Date: " + cal1.getTime());
            }
            return dates;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜