您的位置:首页 > 编程语言 > Java开发

第一篇博客:获取当前日期所在季度的第一个月的第一天和最后一个月的最后一天

2013-09-17 17:55 573 查看
 public static void main(String[] args) {

  DateTime day = new DateTime(2013, 1, 1, 0, 0);

  while(day.getYear() == 2013) {

   System.out.print(day + "     ");

   System.out.print(getQuarterInMonth(day, true) + "    ");;

   System.out.println(getQuarterInMonth(day, false));;

   day = day.plusDays(1);

  }

 }

 private static DateTime getQuarterInMonth(DateTime time, boolean isQuarterStart) {

  int month = time.getMonthOfYear();//获取当前时间的月份

  month =  (month / 3 + (month % 3 == 0 ? 0 : 1)) * 3 - 2;//当前月份是第几季度

  int year = time.getYear();//当前时间的年份

  if(!isQuarterStart) {

   month += 3;

  }

  if(month == 13) {

   year += 1;

   month = 1;

  }

  return new DateTime(year, month, 1, 0, 0);

 }

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐