java - 假如今天周一,我想把过去七天的周几存在一个集合里面,怎么计算。

 可爱沉默999 发布于 2022-10-27 02:20

已经得到今天是周一了,然后把最近七天存入一个集合。

今天周一集合是这样的:[周二,周三,周四,周五,周六,周天,周一]
假如今天周二集合这样:[周三,周四,周五,周六,周天,周一,周二]

通过:

 Calendar c = Calendar.getInstance();
            Format f = new SimpleDateFormat("E");
           String weekday = f.format(c.getTime());
       

已经拿到今天是周几了。有什么好办法能实现我上面说的集合排列吗?

2 个回答
  • 听不懂你说的说意思,,,都能得到今天了,日期往后一直循环加到7不就行了嘛。。

    2022-10-27 02:20 回答
  • public class Test {
    
        private static final String[] CN_WEEKDAYS = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    
        public static String[] getLatest7Days() {
            Calendar c = Calendar.getInstance();
            int w = c.get(Calendar.DAY_OF_WEEK);
            String[] weekdays = new String[7];
            int len = 7 - w;
            System.arraycopy(CN_WEEKDAYS, w, weekdays, 0, len);
    
            for (int i = 0; i < w; i++) {
                weekdays[len++] = CN_WEEKDAYS[i];
            }
            return weekdays;
        }
    
        public static void main(String[] args) {
            String[] days = getLatest7Days();
            System.out.println(Arrays.toString(days));
        }
    }
    2022-10-27 02:20 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有