java 一年有多少周_java获取一年的周数和间隔天数

1 importjava.text.ParseException;2 importjava.text.SimpleDateFormat;3 import java.util.*;4

5 public classTest {6

7 private staticDate deformatDatetime(String strDate, String fmt) {8 try{9 if (fmt == null) {10 return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",11 java.util.Locale.ENGLISH)).parse(strDate);12 } else{13 return (newSimpleDateFormat(fmt, java.util.Locale.ENGLISH))14 .parse(strDate);15 }16 } catch(ParseException e) {17 //TODO Auto-generated catch block

18 e.printStackTrace();19 return null;20 }21 }22 private staticString datetimeToString(Date dt, String fmt) {23 if (fmt == null) {24 return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",25 java.util.Locale.ENGLISH)).format(dt);26 } else{27 return (newSimpleDateFormat(fmt, java.util.Locale.ENGLISH))28 .format(dt);29 }30 }31 private static classDateRange{32 publicDate startDate;33 publicDate endDate;34 publicDateRange(){35

36 }37 publicDateRange(Date dt1,Date dt2){38 startDate=dt1;39 endDate=dt2;40 }41 }42 /**

43 * 根據年份獲得該年所有周次及每周的開始-結束日期44 *@parampvnYear45 *@return

46 */

47 private static LinkedHashMap getWeeksDetByYear(intpvnYear){48 LinkedHashMap lvRet=new LinkedHashMap();49 Calendar lvCal=Calendar.getInstance();50 lvCal.setFirstDayOfWeek(Calendar.MONDAY);51

52 Date lvDt=deformatDatetime(String.valueOf(pvnYear)+"-01-01 00:00:00", null);53 lvCal.setTime(lvDt);54 int lvWeek=1;55 while (true) {56 lvCal.set(Calendar.DAY_OF_WEEK, lvCal.getFirstDayOfWeek()); //Monday

57 Date lvFirstDt=lvCal.getTime();58 if (lvFirstDt.getYear()+1900pvnYear) break;62 lvCal.set(Calendar.DAY_OF_WEEK, lvCal.getFirstDayOfWeek()+6); //Sunday?

63 Date lvLastDt=lvCal.getTime();64 if (lvLastDt.getYear()+1900>pvnYear){65 lvLastDt=deformatDatetime(String.valueOf(pvnYear+1)+"-01-01 00:00:00", null);66 lvCal.setTime(lvLastDt);67 lvCal.add(Calendar.DAY_OF_YEAR, -1);68 lvLastDt=lvCal.getTime();69 }70 lvRet.put(Integer.valueOf(lvWeek), newDateRange(lvFirstDt, lvLastDt));71 lvWeek++;72 lvCal.add(Calendar.WEEK_OF_YEAR, 1);73 }74 returnlvRet;75 }76 public static void main(String[] args) throwsException{77 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");78 LinkedHashMap lvRet=getWeeksDetByYear(2011);79 for (Map.Entryitem:lvRet.entrySet()){80 String startDate = datetimeToString(item.getValue().startDate,"yyyy-MM-dd");81 String endDate = datetimeToString(item.getValue().endDate,"yyyy-MM-dd");82 Date date1 =format.parse(startDate);83 Date date2 =format.parse(endDate);84 int diff =differentDays(date1,date2);85 System.out.println(String.format("Week: %d, %s - %s", item.getKey(),datetimeToString(item.getValue().startDate,"yyyy-MM-dd"),86 datetimeToString(item.getValue().endDate,"yyyy-MM-dd"))+" ----------"+diff);87 }88 }89

90 public static intdifferentDays(Date date1,Date date2)91 {92 Calendar cal1 =Calendar.getInstance();93 cal1.setTime(date1);94

95 Calendar cal2 =Calendar.getInstance();96 cal2.setTime(date2);97 int day1=cal1.get(Calendar.DAY_OF_YEAR);98 int day2 =cal2.get(Calendar.DAY_OF_YEAR);99

100 int year1 =cal1.get(Calendar.YEAR);101 int year2 =cal2.get(Calendar.YEAR);102 if(year1 != year2) //同一年

103 {104 int timeDistance = 0;105 for(int i = year1 ; i < year2 ; i ++)106 {107 if(i%4==0 && i%100!=0 || i%400==0) //闰年

108 {109 timeDistance += 366;110 }111 else //不是闰年

112 {113 timeDistance += 365;114 }115 }116

117 return timeDistance + (day2-day1) ;118 }119 else //不同年

120 {121 return day2-day1;122 }123 }124 }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论

打赏作者

纤雀

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值