您的位置:首页 > 编程语言 > Java开发

获取当前日期上一周开始时间、结束时间和年份

2017-05-25 09:17 375 查看
private Map<String, Object> getParams(String dateStr)
{
Map<String, Object> map = new HashMap<String, Object>();
SimpleDateFormat sbf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sbf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
// 当前年份
int year = 0;
// 年的周数
int week_index = 0;
// 周开始时间
long start = 0L;
// 周结束时间
long end = 0L;
try
{
cal.setTime(sbf.parse(dateStr));
year = cal.get(Calendar.YEAR);
week_index = cal.get(Calendar.WEEK_OF_YEAR);
Date date = null;
String s = "";
// 获取上周周一的开始时间
cal.add(Calendar.WEEK_OF_YEAR, -1);
// 设置日期为周一
cal.set(Calendar.DAY_OF_WEEK, 2);
date = cal.getTime();
s = sbf.format(date) + " 00:00:00";
start = sbf1.parse(s).getTime() / 1000;
// 获取上周周日结束时间
cal.add(Calendar.WEEK_OF_YEAR, 1);
// 设置日期为周日
cal.set(Calendar.DAY_OF_WEEK, 1);
date = cal.getTime();
s = sbf.format(date) + " 23:59:59";
end = sbf1.parse(s).getTime() / 1000;
}
catch (Exception e)
{
LOG.info(e.getMessage(), e);
}
map.put("year", year);
map.put("week_index", week_index);
map.put("week_start", start);
map.put("week_end", end);
return map;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java
相关文章推荐