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;
}



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值