Android周查看我们如何获取下一周和上周的所有日子

时间:2014-04-08 04:28:37

标签: java android

我能够获得当前的工作日期,但如何列出上一周/下周工作日?

这是方法

     public String [] getWeekDay()
    {

        Calendar now = Calendar.getInstance();

        SimpleDateFormat format = new SimpleDateFormat("dd");
        String [] days = new String[7];
        int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 1;
        now.add(Calendar.DAY_OF_MONTH , delta);
        for (int i = 0; i < 7; i++)
            {
                days [i] = format.format(now.getTime());
                now.add(Calendar.DAY_OF_MONTH , 1);
            }
        // System.out.println(Arrays.toString(days));

        return days;

    }

请参阅图片,并告诉我如何获得下一周和上一周的工作日

enter image description here

2 个答案:

答案 0 :(得分:3)

最后我得到了答案请看这个

获取本周:

    public String [] getWeekDay()
    {

        Calendar now = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String [] days = new String[7];
        int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 1;
        now.add(Calendar.DAY_OF_MONTH , delta);
        for (int i = 0; i < 7; i++)
            {
                days [i] = format.format(now.getTime());
                now.add(Calendar.DAY_OF_MONTH , 1);
            }

        return days;

    }

获取下周:

     int weekDaysCount=0;

          public String [] getWeekDayNext()
    {

        weekDaysCount++;
        Calendar now1 = Calendar.getInstance();
        Calendar now = (Calendar) now1.clone();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String [] days = new String[7];
        int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 1;
        now.add(Calendar.WEEK_OF_YEAR , weekDaysCount);
        now.add(Calendar.DAY_OF_MONTH , delta);
        for (int i = 0; i < 7; i++)
            {
                days [i] = format.format(now.getTime());
                now.add(Calendar.DAY_OF_MONTH , 1);
            }

        return days;

    }

获取前一周:

  public String [] getWeekDayPrev()
    {

        weekDaysCount--;
        Calendar now1 = Calendar.getInstance();
        Calendar now = (Calendar) now1.clone();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String [] days = new String[7];
        int delta = -now.get(GregorianCalendar.DAY_OF_WEEK) + 1;
        now.add(Calendar.WEEK_OF_YEAR , weekDaysCount);
        now.add(Calendar.DAY_OF_MONTH , delta);
        for (int i = 0; i < 7; i++)
            {
                days [i] = format.format(now.getTime());
                now.add(Calendar.DAY_OF_MONTH , 1);
            }

        return days;

    }

分配textView

       NextPreWeekday = getWeekDay();
        firstDayOfWeek = CommonMethod.convertWeekDays(NextPreWeekday [0]);
        lastDayOfWeek = CommonMethod.convertWeekDays(NextPreWeekday [6]);
        textViewDate.setText(firstDayOfWeek + "-" + lastDayOfWeek + " " + CommonMethod.convertWeekDaysMouth(NextPreWeekday [6]));

        textViewSun.setText(CommonMethod.convertWeekDays(NextPreWeekday [0]) + "\nSun");
        textViewMon.setText(CommonMethod.convertWeekDays(NextPreWeekday [1]) + "\nMon");
        textViewTue.setText(CommonMethod.convertWeekDays(NextPreWeekday [2]) + "\nTue");
        textViewWed.setText(CommonMethod.convertWeekDays(NextPreWeekday [3]) + "\nWeb");
        textViewThu.setText(CommonMethod.convertWeekDays(NextPreWeekday [4]) + "\nThu");
        textViewFri.setText(CommonMethod.convertWeekDays(NextPreWeekday [5]) + "\nFri");
        textViewSat.setText(CommonMethod.convertWeekDays(NextPreWeekday [6]) + "\nSat");







 public static String convertWeekDays(String date)
    {
        String formattedDate = null;
        try
            {
                SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd" , Locale.ENGLISH);
                SimpleDateFormat targetFormat = new SimpleDateFormat("dd");
                Date date12 = originalFormat.parse(date);
                formattedDate = targetFormat.format(date12);
            } catch (Exception e)
            {
                e.printStackTrace();
            }

        return formattedDate;

    }



public static String convertWeekDaysMouth(String date)
    {
        String formattedDate = null;
        try
            {
                SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd" , Locale.ENGLISH);
                SimpleDateFormat targetFormat = new SimpleDateFormat("MMM yyyy");
                Date date12 = originalFormat.parse(date);
                formattedDate = targetFormat.format(date12);
            } catch (Exception e)
            {
                e.printStackTrace();
            }

        return formattedDate;

    }

答案 1 :(得分:2)

本周。

SimpleDateFormat displayDate = new SimpleDateFormat("dd-MMM-yyyyy"));

        final Calendar calenderThisWeek = Calendar.getInstance();
        calenderThisWeek.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String strWek = displayDate.format(calenderThisWeek.getTime()); // dd-mmm-yyyy
        calenderThisWeek.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);  
        String endWek = displayDate.format(calenderThisWeek.getTime()); // dd-mmm-yyyy

前一周

        final Calendar calenderpreviousWeek = Calendar.getInstance();
        calenderpreviousWeek.add(Calendar.DAY_OF_WEEK, -1);
        calenderpreviousWeek.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String strWek = displayDate.format(calenderThisWeek.getTime()); // dd-mmm-yyyy
        calenderpreviousWeek.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);          
        String endWek = displayDate.format(calenderpreviousWeek.getTime()); // dd-mmm-yyyy  

下周

        final Calendar calenderNextWeek = Calendar.getInstance();
        calenderNextWeek.add(Calendar.DAY_OF_WEEK, +1);
        calenderNextWeek.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        String strWek = displayDate.format(calenderNextWeek.getTime()); // dd-mmm-yyyy
        calenderNextWeek.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);  
        String endWek = displayDate.format(calenderNextWeek.getTime()); // dd-mmm-yyyy