Date
1、获取两个日期之间的所有日期
1 package com.boyutec.framework.command.test; 2 3 import java.text.DateFormat; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Calendar; 7 import java.util.Date; 8 9 public class Test { 10 public static void main(String[] args) { 11 DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 12 try { 13 Date start = dateFormat.parse("2019-01-01"); 14 Date end = dateFormat.parse("2019-02-01"); 15 Calendar tempStart = Calendar.getInstance(); 16 tempStart.setTime(start); 17 Calendar tempEnd = Calendar.getInstance(); 18 tempEnd.setTime(end); 19 tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束) 20 while (tempStart.before(tempEnd)){ 21 System.out.println(dateFormat.format(tempStart.getTime())); 22 tempStart.add(Calendar.DAY_OF_YEAR, 1); 23 } 24 } catch (ParseException e1) { 25 e1.printStackTrace(); 26 } 27 } 28 }
2、获取当前月份、当前周数
1 Calendar cl = Calendar.getInstance(); 2 cl.setFirstDayOfWeek(Calendar.MONDAY);// 默认是从周日开始算第一天,加上这个就是从周一开始算一周的第一天 3 cl.setTime(sdf.parse(dateStr)); 4 int weekNum = cl.get(Calendar.WEEK_OF_YEAR);// 当前周数 5 int year = cl.get(Calendar.YEAR);//当前年份