zoukankan      html  css  js  c++  java
  • 关于时间的循环

    方法一:

    	// 日期用Calendar 比较方便操作, 也不用自己去处理每个月最大天数之类的问题.
    		Calendar calendar = new GregorianCalendar();
    		for (int i = 0; i < 7; i++) {
    			int year = calendar.get(Calendar.YEAR);
    			int month = calendar.get(Calendar.MONTH) + 1; // Jan = 0, not 1
    			int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
    			System.out.println(year + "/" + month + "/" + dayOfMonth);
    			calendar.add(Calendar.DAY_OF_WEEK, 1);
    		}
    	
    

    输出:

    2017/10/20
    2017/10/21
    2017/10/22
    2017/10/23
    2017/10/24
    2017/10/25
    2017/10/26
    ------------------

    方法二  

    		try {
    			Date d1 = new SimpleDateFormat("yyyy-MM-dd").parse("2017-10-20");// 定义起始日期
    			Date d2 = new SimpleDateFormat("yyyy-MM-dd").parse("2017-11-01");// 定义结束日期
    			Calendar dd = Calendar.getInstance();// 定义日期实例
    			dd.setTime(d1);// 设置日期起始时间
    			while (dd.getTime().before(d2)) {// 判断是否到结束日期
    				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    				String str = sdf.format(dd.getTime());
    				System.out.println(str);// 输出日期结果
    				dd.add(Calendar.DATE, 1);// 进行当前日期天数加1
    			//	dd.add(Calendar.MONTH,1);//进行当前日期月份加1
    			//	dd.add(calendar.YEAR,1);//进行当前日期年份加1
    			}
    		} catch (Exception e) {
    		}
    

    输出:

    2017-10-20
    2017-10-21
    2017-10-22
    2017-10-23
    2017-10-24
    2017-10-25
    2017-10-26
    2017-10-27
    2017-10-28
    2017-10-29
    2017-10-30
    2017-10-31

  • 相关阅读:
    firewall
    mtu测试
    bps和pps
    PC上装VM上装虚拟机
    jira添加工作流
    【Linux系列】Centos 7安装 PHP(四)
    【Linux系列】Centos 7安装 Nginx(三)
    【Linux系列】配置Centos 7的软件源(二)
    【Linux系列】Centos 7安装以及网络配置(一)
    Python3获取豆瓣图书标签的前20本热门书籍(一)
  • 原文地址:https://www.cnblogs.com/jdw12306/p/7698101.html
Copyright © 2011-2022 走看看