java时间计算,获取某月第一天和最后一天
2024-08-25 03:47:18
//获取前月的第一天
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
//获取当前月第一天:
Calendar c = Calendar.getInstance(); String first = null;//第一天
String last = null;//第二天
//判断获取的参数如果为-1
if(month.equals("-1")){
c.add(Calendar.MONTH, -1);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
first = sdf.format(c.getTime()); //获取上月的最后一天
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, -1);
last = sdf.format(calendar.getTime());
}else{
c.add(Calendar.MONTH, 0);
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
first = sdf.format(c.getTime()); //获取当前月最后一天
Calendar ca = Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
last = sdf.format(ca.getTime());
} //循环时间段
Date startDate = sdf.parse(first); Date endDate = sdf.parse(last); Calendar start = Calendar.getInstance();
start.setTime(startDate);
Long statTime = start.getTimeInMillis(); Calendar end = Calendar.getInstance();
end.setTime(endDate);
Long endTime = end.getTimeInMillis(); Long oneDay = 1000 * 60 * 60 * 24l; Long time = statTime; List timeList=new ArrayList();
while (time <= endTime) {
Date d = new Date(time);
DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
time += oneDay;
timeList.add(df.format(d));
} //根据渠道ID查询当前代理公司下有多少交易成功的单 String lastDate = null;
if(month.equals("-1")){
//获取这个时间段下一天
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.DATE, 0);
lastDate = sdf.format(calendar.getTime());
}else{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.add(Calendar.MONTH, 1);
lastDate = sdf.format(calendar.getTime());
}
这是获取时间,然后这个时间进行相加
比如
updateDate = "2016-11-9"
//得到保单状态最后修改的时间
Date updateDate = tranCar.getDtUpdateDate();
//计算时间,+3天
Calendar calendar = Calendar.getInstance();
calendar.setTime(updateDate);
calendar.add(calendar.DATE,3);
//得出的结果
updateDate = "2016-11-12"
//获取上个月25号的时间
private String setDayLastMonth(SimpleDateFormat format) {
Calendar month = Calendar.getInstance();
month.add(Calendar.MONTH,-1);
month.set(Calendar.DATE,25);
String oneDayLastMonth = format.format(month.getTime());
return oneDayLastMonth;
} //获取当月的时间,这里时间不能超过当月25号
private String setDayBefore(SimpleDateFormat format) {
Calendar cal = Calendar.getInstance();//获取一个日历
cal.add(Calendar.DAY_OF_MONTH, -1);//取当前日期的前一天. //当前时间
String today = format.format(Calendar.getInstance().getTime());
String theDayBefore = format.format(cal.getTime());
//比较时间大小
Date theDayBeforeDate = cal.getTime();//当前时间的前一天日期格式
Date todayDate = Calendar.getInstance().getTime();//当前时间日期格式 if(theDayBeforeDate.getTime() > todayDate.getTime()){
//默认日期只能为本月的24日
cal.set(Calendar.DATE,24);
theDayBefore = format.format(cal.getTime());
}
return theDayBefore;
}
最新文章
- form表单序列化后的数据转json对象
- 3.Java基础之Date对象
- .Net 4.5 的async 和await 的简单理解使用
- 使用staruml学习画类图
- 导出程序界面(UI)到图片
- Linux中常用的查看系统信息的命令
- 【No.1】监控Linux性能25个命令行工具
- Sequential Read Ahead For SQL Server
- 模拟QQ侧滑控件 实现三种界面切换效果(知识点:回调机制,解析网络json数据,fragment用法等)。
- php多文本框提交
- 10. leetcode 226 Invert Binary Tree
- [日常] AtCoder Beginner Contest 075 翻车实录
- go [第一篇]初识
- windows下安装bpython方法 (新)
- [daily] cscope
- Vue(二) 计算属性
- IAR EWARM 关闭纯汇编函数的警告的方法
- java 基本数据类型及自动类型提升
- Streamr助你掌控自己的数据
- MapReduce编程之Map Join多种应用场景与使用