Android Spinner显示周月跨度列表

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

 

 

 

如图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值