Android Spinner显示周月跨度列表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chunho/article/details/51360338

1.获取周跨度列表

    直接调用getWeekList(0)这个方法即可,得到周列表

/**
 * 获取周日期列表
 */
public static ArrayList<String> getWeekList(int startId) {
    ArrayList<String> list = new ArrayList<String>();
    Date today = new Date();
    String startDate = "";
    String endDate = "";
    for (int i = 0; i < 12 + startId; i++) {//12为默认显示的条数
        startDate = dateToString(getWeekList(today).get(0));
        endDate = dateToString(getWeekList(today).get(6));
        Long fTime = today.getTime() - 7 * 24 * 3600000;
        today.setTime(fTime);
        list.add(startDate + " ~ " + endDate);
    }
    return list;
}

private static String dateToString(Date mdate) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
    String strDate = sdf.format(mdate);
    return strDate;
}

private static List<Date> dateToWeek(Date mdate) {
    int b = mdate.getDay();
    Date fdate;
    List<Date> list = new ArrayList<Date>();
    Long fTime = mdate.getTime() - b * 24 * 3600000;
    for (int a = 1; a <= 7; a++) {
        fdate = new Date();
        fdate.setTime(fTime + (a * 24 * 3600000));
        list.add(a - 1, fdate);
    }
    return list;
}

private static ArrayList<Date> getWeekList(Date mdate) {

    ArrayList<Date> days = new ArrayList<>();
    days.addAll(dateToWeek(mdate));

    Calendar calendar = Calendar.getInstance();
    int lastIndex = days.size() - 1;
    Date startDay = days.get(0);
    Date endDay = days.get(lastIndex);

    calendar.setTime(startDay);
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    days.remove(0);
    days.add(0, calendar.getTime());

    calendar.clear();
    calendar.setTime(endDay);
    calendar.set(Calendar.HOUR_OF_DAY, 23);
    calendar.set(Calendar.MINUTE, 59);
    calendar.set(Calendar.SECOND, 59);
    days.remove(lastIndex);
    days.add(lastIndex, calendar.getTime());

    return days;
}

如图:



2.获取月跨度列表

    直接调用getMonthList(0)这个方法即可,得到月列表

/**
 * 根据年月获取该月份的天数
 *
 * @return
 */
public static int getDayOfMonth(int year, int month) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    calendar.set(Calendar.MONTH, month - 1);
    int dayOfMonth = calendar.getActualMaximum(Calendar.DATE);
    return dayOfMonth;
}

/**
 * 获取月跨度列表
 * @param startId
 * @return
 */
public static ArrayList<String> getMonthList(int startId) {
    ArrayList<String> list = new ArrayList<String>();
    Calendar calendar = Calendar.getInstance();
    String startDate = "";
    String endDate = "";
    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH) + 1;
    for (int i = 0; i < 12 + startId; i++) {
        //月份显示两位数
        String strMonth = month < 10 ? "0" + month : "" + month;
        startDate = year + "." + strMonth + "." + "01";
        endDate = year + "." + strMonth + "." + getDayOfMonth(year, month);
        if (month > 1) {
            month--;
        } else {
            year--;
            month = 12;
        }
        list.add(startDate + " ~ " + endDate);
    }
    return list;
}

如图:

阅读更多

没有更多推荐了,返回首页