Java 日期處理方法

import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;


public class DealData {
/**
* 獲取兩個日期區間的所有周,並指定周所在的月份
* @param sQsrq
* @param sJzrq
* @throws ParseException
*/
public static List<String> getWeeksByTwoRq(String sQsrq, String sJzrq) throws ParseException {
List<String> reList = new ArrayList<String>();
Calendar c_begin = new GregorianCalendar();
Calendar c_end = new GregorianCalendar();
DateFormatSymbols dfs = new DateFormatSymbols();
String[] weeks = dfs.getWeekdays();
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
// 獲取起始日期和截至日期周的週六
start.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(getWeekEndDate(sQsrq)));
end.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(getWeekEndDate(sJzrq)));
c_begin.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH),start.get(Calendar.DATE)); // Calendar的月從0-11,所以4月是3.
c_end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH),end.get(Calendar.DATE)); // Calendar的月從0-11,所以5月是4.
        c_end.add(Calendar.DAY_OF_YEAR, 1); // 結束日期下滾一天是爲了包含最後一天
int iWeek = c_begin.get(Calendar.WEEK_OF_YEAR);
int iYear = c_begin.get(Calendar.YEAR);


while (c_begin.before(c_end)) {
if (iYear != c_begin.get(Calendar.YEAR)) {
iWeek = 1;
iYear = c_begin.get(Calendar.YEAR);
}
if (weeks[c_begin.get(Calendar.DAY_OF_WEEK)].equals("星期六")) {
String reString = c_begin.get(Calendar.YEAR) + "年" + (c_begin.get(Calendar.MONTH) + 1) + "月第" + iWeek + "周";
reList.add(reString);
}
if (c_begin.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
iWeek++;
}
c_begin.add(Calendar.DAY_OF_YEAR, 1);
}


return reList;
}


/**
* 獲取指定日期所在周的週六的日期
* @param sDate
* @return
* @throws ParseException
*/
public static String getWeekEndDate(String sDate) throws ParseException// 這裏傳遞選定的日期
{
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(sDate));
int d = 7 - cal.get(Calendar.DAY_OF_WEEK);

cal.add(Calendar.DAY_OF_WEEK, d);
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}

/**
* 獲取指定日期所在周的指定周幾的日期的日期
* @param sDate
* @return
* @throws ParseException
*/
public static String getWeekNumDate(String sDate, int iNum) throws ParseException// 這裏傳遞選定的日期
{
if(iNum<1 || iNum >7)
{
System.out.println("無法獲取本週第" + iNum + "天");
}
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(sDate));
int d = iNum - cal.get(Calendar.DAY_OF_WEEK);

cal.add(Calendar.DAY_OF_WEEK, d);
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}

/**
* 獲取指定日期所在月的第i天
* @param sDate
* @return
* @throws ParseException
*/
public static String getMonthNumDate(String sDate, int iNum) throws ParseException// 這裏傳遞選定的日期
{
Calendar cal = Calendar.getInstance();
cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(sDate));
int d = iNum - cal.get(Calendar.DAY_OF_MONTH);

cal.add(Calendar.DAY_OF_MONTH, d);
return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
}

/**
* 獲取兩個日期區間的所有月
* @param sQsrq
* @param sJzrq
* @throws ParseException
*/
public static List<String> getMonthsByTwoRq(String sQsrq, String sJzrq) throws ParseException {
List<String> reList = new ArrayList<String>();
Calendar c_begin = new GregorianCalendar();
Calendar c_end = new GregorianCalendar();
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
// 獲取起始日期和截至日期周的週六
start.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(sQsrq));
end.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(sJzrq));
c_begin.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH),start.get(Calendar.DATE)); // Calendar的月從0-11,所以4月是3.
c_end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH),end.get(Calendar.DATE)); // Calendar的月從0-11,所以5月是4.
       // c_end.add(Calendar.DAY_OF_YEAR, 1); // 結束日期下滾一天是爲了包含最後一天


        String szMess = c_begin.get(Calendar.YEAR) + "年" + (c_begin.get(Calendar.MONTH)+ 1 )+ "月";
        reList.add(szMess);
while (c_begin.before(c_end)) {
c_begin.add(Calendar.DAY_OF_YEAR, 1);
String curMess = c_begin.get(Calendar.YEAR) + "年" + (c_begin.get(Calendar.MONTH)+ 1 )+ "月";
if(!szMess.equals(curMess))
{
szMess = curMess;
reList.add(szMess);
}
}
return reList;
}



}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章