JAVA自定义时间段字符数组

 最终的结果:

2019/08/20-2020/08/20
2018/08/20-2019/08/20
2017/08/20-2018/08/20

代码: 

String[] customDate = new String[3];
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Calendar now = Calendar.getInstance();
now.setTime(date);

for(int i = 2; i >= 0; i--){
  StringBuffer str = new StringBuffer();
  now.add(Calendar.YEAR, -1);
  Date dateOne = now.getTime();
  str.append(sdf.format(dateOne))
     .append("-");
  now.add(Calendar.YEAR, 1);
  Date dateTwo = now.getTime();
  str.append(sdf.format(dateTwo));
  customeDate[i] = str.toString();
  now.add(Calendar.YEAR, -1);
  System.out.println(arrOne[i]);
}

 

你可能感兴趣的:(菜鸟逆袭之路——工具)