第一,你沒有考慮平年閏年的問題,閏年二月有29天

創(chuàng)新互聯公司自成立以來,一直致力于為企業(yè)提供從網站策劃、網站設計、網站制作、做網站、電子商務、網站推廣、網站優(yōu)化到為企業(yè)提供個性化軟件開發(fā)等基于互聯網的全面整合營銷服務。公司擁有豐富的網站建設和互聯網應用系統(tǒng)開發(fā)管理經驗、成熟的應用系統(tǒng)解決方案、優(yōu)秀的網站開發(fā)工程師團隊及專業(yè)的網站設計師團隊。
第二,你的break放在return之后,java編譯器會報錯,看你寫代碼這么繞,java基礎應該不差啊,那么問你,return之后的語句有意義嗎?
第三,打印流在整個類中的使用中如果關閉一次,下次就無法打開,所以最后再關閉打印流,在輸入月份的時候不要關閉打印流,就你的input.close()去掉,完了就對了
給定一個時間范圍判斷周的范圍并且跨年為新的一周即跨年時分為兩周
public class Week{
public static void main(String[] args) {
SimpleDateFormat myFormatterExt = new SimpleDateFormat("yyyy-MM-dd");
Date b_date = null;
Date e_date = null;
try {
b_date = myFormatterExt.parse("2010-12-26");//開始時間
e_date = myFormatterExt.parse("2011-01-06");//結束時間
//e_date = myFormatterExt.parse("2011-01-08");
// date= myFormatterExt.parse("2010-12-31 00:00:00");
// String week = HNFDCommon.getWeek(date);
//System.out.println(week+"week");
} catch (ParseException e) {
e.printStackTrace();
}
b_date = getFirstDayOfWeek(b_date);
e_date = getLastDayOfWeek(e_date);
int dayOfweek = 0; //日期為星期幾判斷12月最后一個星期用
while (b_date.getTime() == e_date.getTime() || b_date.before(e_date))
{
String week = HNFDCommon.getWeek(b_date);
System.out.println(week+"ddd"+myFormatterExt.format(b_date));
String days = myFormatterExt.format(b_date).substring(8,10);
if(dayOfweek0){
b_date.setDate(b_date.getDate() +(7-dayOfweek));
dayOfweek = 0;
}else{
if(Integer.parseInt(week.substring(5,7))=50 (31- Integer.parseInt(days))7){
Date c_date = new Date();
c_date.setYear(b_date.getYear());
c_date.setMonth(b_date.getMonth());
c_date.setDate(31);
dayOfweek = getDayOfWeek(c_date);
b_date.setDate(b_date.getDate() +dayOfweek-1);
if(week.equals(HNFDCommon.getWeek(b_date))){
b_date.setDate(b_date.getDate() +(7-dayOfweek));
dayOfweek = 0;
}
}
else{
b_date.setDate(b_date.getDate() +7);
}
}
}
}
public static Date getLastDayOfWeek(Date date) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy");
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()+6); // Sunday
if(!sd.format(date).equals(sd.format(c.getTime()))){
c.setTime(date);
}
return c.getTime();
}
public static Date getFirstDayOfWeek(Date date) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy");
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Sunday
if(!sd.format(date).equals(sd.format(c.getTime()))){
c.setTime(date);
}
return c.getTime();
}
public static int getDayOfWeek(Date date) {
Calendar c = new GregorianCalendar();
c.setFirstDayOfWeek(Calendar.SUNDAY);
c.setTime(date);
//c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
return c.get(Calendar.DAY_OF_WEEK);
}
}
HNFDCommon 類中:
public static String getWeek(Date date)
{
String rtn="";
int year;
int week;
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
Calendar cd = Calendar.getInstance();
cd.setTime(date);
year=cd.get(Calendar.YEAR);
week = cd.get(Calendar.WEEK_OF_YEAR);
//System.out.println("ttt"+sd.format(cd.getTime())+"bbb"+week);
int vmonth = cd.get(Calendar.MONTH)+1;
if(week == 1 vmonth == 12){
cd.add(Calendar.WEEK_OF_YEAR,-1);
week =cd.get(Calendar.WEEK_OF_YEAR)+1;
}
if(week 6 vmonth == 1){
year--;
}
if(week 10){
rtn=year+"00"+week;
}
else{
rtn=year+"0"+week;
}
return rtn;
}
用java的日歷類
public static void main(String[] args) throws ParseException {
String d1= "2011-05-12";
String d2="2011-08-15";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = format.parse(d1);
Date date2 = format.parse(d2);
Calendar cal =Calendar.getInstance();
cal.setTime(date1);
int day = cal.get(Calendar.DAY_OF_MONTH);
while(cal.getTime().before(date2)){
String begin = format.format(cal.getTime());
int max = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, max);
String end = cal.getTime().before(date2) ? format.format(cal.getTime()) : d2;
System.out.println(begin+"~"+end);
cal.add(Calendar.MONTH, 1);
cal.set(Calendar.DAY_OF_MONTH, day);
}
}
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;
public class DateTest {
/**
* 判斷是否在同一個月
* @param startDate yyyy-MM-dd
* @param endDate yyyy-MM-dd
* @return false:不在同一個月內,true在同一個月內
*/
public static boolean isMonth(String startDate,String endDate){
if(margin(startDate, endDate)31){
return false;
}
int startMonth = Integer.parseInt(startDate.substring(5, 7));
int endMonth = Integer.parseInt(endDate.substring(5, 7));
if(startMonth==endMonth){
return true;
}else{
return false;
}
}
/**
* 計算開始日期和結束日期差
* @param startDate yyyy-MM-dd
* @param endDate yyyy-MM-dd
* @return
*/
private static int margin(String startDate,String endDate){
ParsePosition pos = new ParsePosition(0);
ParsePosition pos2 = new ParsePosition(0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date ds = sdf.parse(startDate, pos);
Date de = sdf.parse(endDate, pos2);
long l = de.getTime()-ds.getTime();
int margin = (int)(l/24*60*60*1000);
return margin;
}
/**
* main方法測試
* @param args
*/
public static void main(String[] args) {
System.out.println(DateTest.isMonth("2014-10-17", "2014-10-25"));
System.out.println(DateTest.isMonth("2014-10-17", "2014-12-25"));
}
}
/**
?*?獲取兩個日期相差的天數
?*?@param?big
?*?@param?small
?*?@return
?*/
public?static?int?getTwoDatesDifOfDay(Date?big,Date?small){
Calendar?cal1?=?Calendar.getInstance();
cal1.setTime(big);
Calendar?cal2?=?Calendar.getInstance();
cal2.setTime(small);
if(cal1.get(Calendar.MONTH)?!=?11??cal2.get(Calendar.MONTH)==11){//跨年
Calendar?cal3?=?Calendar.getInstance();
cal3.set(Calendar.MONTH,?11);
cal3.set(Calendar.DAY_OF_MONTH,?31);
return?cal3.get(Calendar.DAY_OF_YEAR)-cal2.get(Calendar.DAY_OF_YEAR)+cal1.get(Calendar.DAY_OF_YEAR);
}else{
return? cal1.get(Calendar.DAY_OF_YEAR)-cal2.get(Calendar.DAY_OF_YEAR);
}
}
/**
?*?獲取兩個日期相差的周數
?*?@param?big
?*?@param?small
?*?@return
?*/
public?static?int?getTwoDatesDifOfWeek(Date?big,Date?small){
Calendar?cal1?=?Calendar.getInstance();
cal1.setTime(big);
Calendar?cal2?=?Calendar.getInstance();
cal2.setTime(small);
if(cal1.get(Calendar.MONTH)?!=?11??cal2.get(Calendar.MONTH)==11){//跨年
Calendar?cal3?=?Calendar.getInstance();
cal3.set(Calendar.MONTH,?11);
cal3.set(Calendar.DAY_OF_MONTH,?31);
return?cal3.get(Calendar.WEEK_OF_YEAR)-cal2.get(Calendar.WEEK_OF_YEAR)+cal1.get(Calendar.WEEK_OF_YEAR);
}else{
return? cal1.get(Calendar.WEEK_OF_YEAR)-cal2.get(Calendar.WEEK_OF_YEAR);
}
}
網頁標題:java做個跨年代碼 程序員的跨年代碼
新聞來源:http://www.chinadenli.net/article36/hipipg.html
成都網站建設公司_創(chuàng)新互聯,為您提供網站維護、全網營銷推廣、營銷型網站建設、微信公眾號、面包屑導航、定制開發(fā)
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯