簡體   English   中英

如何獲取Android中特定日期的工作日列表

[英]How to I get the list of week days for a specific date in android

我正在Android應用程序中實現物料日歷視圖,用戶可以在Calendar中選擇一個特定的日期。如何獲取該特定所選日期的工作日的完整列表

例如:如果用戶選擇22作為日期,我如何從存在22的日歷中獲取該行中的工作日

您的要求不清楚。 如果要獲取當天的工作日,則可以使用Calendar.getInstance().set(year, month, day); int dayOfWeek = Calendar.getInstance().DAY_OF_WEEK; Calendar.getInstance().set(year, month, day); int dayOfWeek = Calendar.getInstance().DAY_OF_WEEK;

參考: https : //developer.android.com/reference/java/util/Calendar.html

嘗試這個

public String[] getWeekDaySelected(String selectedDateStr) {

        Calendar cal = Calendar.getInstance();

        try {
            Date startDate = sdf.parse(sdf.format(cal.getTime()));
            Date endDate = sdf.parse(selectedDateStr);
            weekDaysCount = Functions.getWeeksBetween(startDate, endDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String[] days = new String[7];
        // set the011, 10 - 1, 12);
        String[] arr = selectedDateStr.split("-");
        cal.set(Calendar.DAY_OF_MONTH, Integer.parseInt(arr[2]));
        cal.set(Calendar.MONTH, (Integer.parseInt(arr[1]) - 1));
        cal.set(Calendar.YEAR, Integer.parseInt(arr[0]));
        Calendar first = (Calendar) cal.clone();
        first.add(Calendar.DAY_OF_WEEK, first.getFirstDayOfWeek() - first.get(Calendar.DAY_OF_WEEK));
        first.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

        // and add six days to the end date
        Calendar last = (Calendar) first.clone();
        last.add(Calendar.DAY_OF_YEAR, 6);

        // print the result
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(df.format(first.getTime()) + " -> " +
                df.format(last.getTime()));

        for (int i = 0; i < 7; i++) {
            days[i] = format.format(first.getTime());
            first.add(Calendar.DAY_OF_MONTH, 1);
        }
        return days;

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM