Android 中本周的开始日期和结束日期

发布于 2024-12-09 14:43:28 字数 410 浏览 0 评论 0原文

我需要在 Android 中找到本周的开始和结束日期。

示例

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

使用c.get(Calendar.WEEK_OF_YEAR); 我可以获取周数,但如何获取开始时间和日期?结束日期?我在这里找到了指向 MonthDisplayHelper 的答案,但如何使用它?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

土豪我们做朋友吧 2024-12-16 14:43:28

使用了这个语法并且它起作用了

    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);  

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);  
错々过的事 2024-12-16 14:43:28

这是很好的示例代码,它为您提供一年中的当前周以及周的开始和结束日期,您需要做的就是在代码中设置一周的开始日期,在我的例子中,我将其设置为 SUNDAY ,

    // get Current Week of the year
    Calendar 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 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=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);
錯遇了你 2024-12-16 14:43:28

这是一个很好的例子,因为它显示了您想要的正确日期: 01 - 07 Jan 8 - 15 Jan ...等等 ...通过按下 BackWard 按钮和相对前进按钮。

p/s : 请根据需要编辑日期格式。

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;
    }

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;
    }
娇女薄笑 2024-12-16 14:43:28

按照此代码这将帮助您找到本周的开始日期和最后日期。
它在我身边工作。

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();

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();
离鸿 2024-12-16 14:43:28

这是 Kotlin 版本:

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

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
北方的巷 2024-12-16 14:43:28

这是为 Kotlin 爱好者准备的!

                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)
                }

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)
                }
月竹挽风 2024-12-16 14:43:28
// <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;
        }
    }
// <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;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文