Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Java SE 综合讨论区
打印话题 寄给朋友 订阅主题 |
作者 | [转载]如何获得指定日期所在周的全部日期 |
jerrylees654321
发贴: 14 积分: 0 |
于 2003-03-21 11:51
问: 已经知道当前日期是2002年4月2号,如何4月2号所在周的所有的日期 答: 使用Calendar 首先你可以根据日期获得是星期几。那么你就可以相应的可以获得星期一(这个星期内的其他日期和当前日期的差额) 我写个方法,你可以使用: 代码: -------------------------------------------------------------------------------- /**获得特定日期多少天前(后)的日前 * @param oldD [Calendar] 特定日期 * @param days [int] 天数 * @param b [boolean][false前][true后] * notice: days<365 * @author: 银狐999 */ public static java.util.Calendar getNewDate(java.util.Calendar oldD,int days,boolean b){ if(days>=365){ return null; } //int nowYear = oldD.getYear(); int nowYear = oldD.get(Calendar.YEAR); //Calendar ca = Calendar.getInstance(); //ca.setTime(oldD); Calendar ca = oldD; int nowDay = ca.get(Calendar.DAY_OF_YEAR); int newYear = 0; int newDay = 0; if{//向后加 newDay = nowDay + days; newYear = nowYear; if(isProYear(nowYear)){ if(newDay>366){ newDay = newDay-366; newYear = nowYear+1; } }else{ if(newDay>365){ newDay = newDay-365; newYear = nowYear+1; } } }else{//向前减 newDay = nowDay-days; newYear = nowYear; if(isProYear(nowYear)){ if(newDay<0){ newDay = newDay+365; newYear = nowYear - 1; } }else{ if(newDay<0){ newDay = newDay+366; newYear = nowYear - 1; } } } java.util.Calendar newDate = getDateByday(newYear,newDay); return newDate; } /**判别是否是润年 * @param year [int] 输入的年份 * @return boolean [ture润年][false非润年] * @author : 银狐999 */ public static boolean isProYear(int year){ boolean isproyear = false; if((year%400 == 0)|(year%100 !=0 && year%4 == 0)){ isproyear = true; }else{ isproyear = false; } return isproyear; } /**根据特定年中的第多少天,获得新日期 * @param year [int] 输入特定年份 * @param day [int] 输入的第多少天 * @return Calendar * @author : 银狐999 */ public static java.util.Calendar getDateByday(int year,int day){ if(day>366){ return null; } boolean isproyear = isProYear(year); int dayofmonth = 0; int i = day; int month = 0; if(isproyear){ //润年情况 if(i<=31){dayofmonth = day; month = 1; } else if(i>31 && i<=60){dayofmonth = day-31; month = 2; } else if(i>60 && i<=91){dayofmonth = day-60; month = 3; } else if(i>91 && i<=121){dayofmonth = day-91; month = 4; } else if(i>121 && i<=152){dayofmonth = day-121; month = 5; } else if(i>152 && i<=182){dayofmonth = day-152; month = 5; } else if(i>182 && i<=213){dayofmonth = day-182; month = 7; } else if(i>213 && i<=244){dayofmonth = day-213; month = 8; } else if(i>244 && i<=274){dayofmonth = day-244; month = 9; } else if(i>274 && i<=305){dayofmonth = day-274; month = 10; } else if(i>305 && i<=335){dayofmonth = day-305; month = 11; } else if(i>335 && i<=366){dayofmonth = day-335; month = 12; } }else{ //非润年情况 if(i<=31){dayofmonth = day; month = 1; } else if(i>31 && i<=59){dayofmonth = day-31; month = 2; } else if(i>59 && i<=90){dayofmonth = day-59; month = 3; } else if(i>90 && i<=120){dayofmonth = day-90; month = 4; } else if(i>120 && i<=151){dayofmonth = day-120; month = 5; } else if(i>151 && i<=181){dayofmonth = day-151; month = 6; } else if(i>181 && i<=212){dayofmonth = day-181; month = 7; } else if(i>212 && i<=243){dayofmonth = day-212; month = 8; } else if(i>243 && i<=273){dayofmonth = day-243; month = 9; } else if(i>273 && i<=304){dayofmonth = day-273; month = 10; } else if(i>304 && i<=334){dayofmonth = day-304; month = 11; } else if(i>334 && i<=365){dayofmonth = day-334; month = 12; } } java.util.Calendar ca = java.util.Calendar.getInstance(); //设定时间从当前的0时0分1秒开始 //所有的月份均是0 based:0-11 ca.set(year,month-1,dayofmonth,0,0,1); return ca; } 为什么这个Applet不能在浏览器显示啊? |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
2512 | [转载]如何获得指定日期所在周的全部日期 | jerrylees654321 | 3751 | 2003-03-21 11:51 |
已读帖子 新的帖子 被删除的帖子 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |