JAVA获取目标日期对应的年,月,季度,周时间类
来源:互联网 发布:sql中having的用法 编辑:程序博客网 时间:2024/06/01 09:57
好记性不如烂笔头
public class TimeUtil { /** * 获取 当前年、半年、季度、月、日、小时 开始结束时间 */ private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH"); private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private final static SimpleDateFormat _shortSdf = new SimpleDateFormat("yyyyMMdd"); /** * 获得本周的第一天,周一 * * @return */ public static Date getCurrentWeekDayStartTime() { Calendar c = Calendar.getInstance(); try { int weekday = c.get(Calendar.DAY_OF_WEEK) - 2; c.add(Calendar.DATE, -weekday); c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00")); } catch (Exception e) { e.printStackTrace(); } return c.getTime(); } /** * 获得目标日期的,周日 * * @return */ public Date getCurrentWeekDayEndTime(String targetDate) { Date from = DateUtils.parseDate(targetDate, DateUtils.FMT_DATE_); Calendar cal = Calendar.getInstance(); cal.setTime(from); try { int weekday = cal.get(Calendar.DAY_OF_WEEK); cal.add(Calendar.DATE, 8 - weekday); cal.setTime(_shortSdf.parse(_shortSdf.format(cal.getTime()))); } catch (Exception e) { e.printStackTrace(); } return cal.getTime(); } /** * 获得本周的最后一天,周日 * * @return */ public static Date getCurrentWeekDayEndTime() { Calendar c = Calendar.getInstance(); try { int weekday = c.get(Calendar.DAY_OF_WEEK); c.add(Calendar.DATE, 8 - weekday); c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59")); } catch (Exception e) { e.printStackTrace(); } return c.getTime(); } /** * 获得本天的开始时间 * * @return */ public static Date getCurrentDayStartTime() { Date now = new Date(); try { now = shortSdf.parse(shortSdf.format(now)); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获得本天的结束时间 * * @return */ public static Date getCurrentDayEndTime() { Date now = new Date(); try { now = longSdf.parse(shortSdf.format(now) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获得本小时的开始时间 * * @return */ public static Date getCurrentHourStartTime() { Date now = new Date(); try { now = longHourSdf.parse(longHourSdf.format(now)); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获得本小时的结束时间 * * @return */ public static Date getCurrentHourEndTime() { Date now = new Date(); try { now = longSdf.parse(longHourSdf.format(now) + ":59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获得本月的开始时间 * * @return */ public static Date getCurrentMonthStartTime() { Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.DATE, 1); now = shortSdf.parse(shortSdf.format(c.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 目标日期月份的结束时间 * * @return */ public Date getCurrentMonthEndTime(String targetDate) { Date from = DateUtils.parseDate(targetDate, DateUtils.FMT_DATE_); Calendar cal = Calendar.getInstance(); cal.setTime(from); Date now = null; try { cal.set(Calendar.DATE, 1); cal.add(Calendar.MONTH, 1); cal.add(Calendar.DATE, -1); now = _shortSdf.parse(_shortSdf.format(cal.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 本月的结束时间 * * @return */ public static Date getCurrentMonthEndTime() { Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.DATE, 1); c.add(Calendar.MONTH, 1); c.add(Calendar.DATE, -1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前年的开始时间 * * @return */ public static Date getCurrentYearStartTime() { Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.MONTH, 0); c.set(Calendar.DATE, 1); now = shortSdf.parse(shortSdf.format(c.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前年的结束时间 * * @return */ public static Date getCurrentYearEndTime() { Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 目标年的结束时间 * * @return */ public Date getCurrentYearEndTime(String targetDate) { Date from = DateUtils.parseDate(targetDate, DateUtils.FMT_DATE_); Calendar c = Calendar.getInstance(); c.setTime(from); Date now = null; try { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); now = _shortSdf.parse(_shortSdf.format(c.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前季度的开始时间 * * @return */ public static Date getCurrentQuarterStartTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) c.set(Calendar.MONTH, 0); else if (currentMonth >= 4 && currentMonth <= 6) c.set(Calendar.MONTH, 3); else if (currentMonth >= 7 && currentMonth <= 9) c.set(Calendar.MONTH, 4); else if (currentMonth >= 10 && currentMonth <= 12) c.set(Calendar.MONTH, 9); c.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 目标日期季度的结束时间 * * @return */ public Date getCurrentQuarterEndTime(String targetDate) { Date from = DateUtils.parseDate(targetDate, DateUtils.FMT_DATE_); Calendar c = Calendar.getInstance(); c.setTime(from); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) { c.set(Calendar.MONTH, 2); c.set(Calendar.DATE, 31); } else if (currentMonth >= 4 && currentMonth <= 6) { c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); } else if (currentMonth >= 7 && currentMonth <= 9) { c.set(Calendar.MONTH, 8); c.set(Calendar.DATE, 30); } else if (currentMonth >= 10 && currentMonth <= 12) { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } now = _shortSdf.parse(_shortSdf.format(c.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前季度的结束时间 * * @return */ public static Date getCurrentQuarterEndTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) { c.set(Calendar.MONTH, 2); c.set(Calendar.DATE, 31); } else if (currentMonth >= 4 && currentMonth <= 6) { c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); } else if (currentMonth >= 7 && currentMonth <= 9) { c.set(Calendar.MONTH, 8); c.set(Calendar.DATE, 30); } else if (currentMonth >= 10 && currentMonth <= 12) { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获取前/后半年的开始时间 * * @return */ public static Date getHalfYearStartTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 6) { c.set(Calendar.MONTH, 0); } else if (currentMonth >= 7 && currentMonth <= 12) { c.set(Calendar.MONTH, 6); } c.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获取目标日期的 前/后半年的结束时间 * * @return */ public Date getHalfYearEndTime(String targetDate) { Date from = DateUtils.parseDate(targetDate, DateUtils.FMT_DATE_); Calendar c = Calendar.getInstance(); c.setTime(from); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 6) { c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); } else if (currentMonth >= 7 && currentMonth <= 12) { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获取前/后半年的结束时间 * * @return */ public static Date getHalfYearEndTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 6) { c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); } else if (currentMonth >= 7 && currentMonth <= 12) { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } public static void main(String[] args) { System.out.println("当前小时开始:" + getCurrentHourStartTime().toString()); System.out.println("当前小时结束:" + getCurrentHourEndTime().toString()); System.out.println("当前天开始:" + getCurrentDayStartTime().toString()); System.out.println("当前天时结束:" + getCurrentDayEndTime().toString()); System.out.println("当前周开始:" + getCurrentWeekDayStartTime().toString()); System.out.println("当前周结束:" + getCurrentWeekDayEndTime().toString()); System.out.println("当前月开始:" + getCurrentMonthStartTime().toString()); System.out.println("当前月结束:" + getCurrentMonthEndTime().toString()); System.out.println("当前季度开始:" + getCurrentQuarterStartTime().toString()); System.out.println("当前季度结束:" + getCurrentQuarterEndTime().toString()); System.out.println("当前半年/后半年开始:" + getHalfYearStartTime().toString()); System.out.println("当前半年/后半年结束:" + getHalfYearEndTime().toString()); System.out.println("当前年开始:" + getCurrentYearStartTime().toString()); System.out.println("当前年结束:" + getCurrentYearEndTime().toString()); }}
阅读全文
0 0
- JAVA获取目标日期对应的年,月,季度,周时间类
- java 获取当前时间的天、周、月、季度、半年度、年度开始和结束时间
- 获取sql server与年、季度、月、周首尾有关的时间函数
- 给定日期和日期类型(年、月、周、日)返回对应日期类型的开始时间和结束时间
- 获取当前时间的天、周、月、季度、半年度、年度开始和结束时间
- C#根据当前时间获取周,月,季度,年度等时间段的起止时间
- Java获取时间范围: 当前季度,上个季度,昨天,当前月,上个月
- Oracle存储过程、触发器实现获取时间段内周、月、季度的具体时间
- java 计算天月季度年的起始结束时间戳(直接运行即可)
- java时间加减法计算,获取指定时间的季度,周数,星期,等;
- 生成日期,周,月,季度,年,财年关系表
- 日期工具类,计算周月季度相关值
- sqlite 取日期的方法:年,季度,月,当月第几周
- sqlite 取日期的方法:年,季度,月,当月第几周
- Java获取时间的年、月、日
- 时间控件:my97DatePicker选择年、季度、月、周、日
- ASP.NET日期时间型算法(月,星期,季度)
- python获取指定日期范围内的每一天,每个月,每季度
- vue2.0路由的配置
- px 与 dp, sp换算公式
- jq动态增加元素时,点击事件不响应解决方法(其他类似)
- linux环境变量
- Arraylist vector linkedList
- JAVA获取目标日期对应的年,月,季度,周时间类
- HDU
- 基于iOS 10封装的下载器(支持存储读取、断点续传、后台下载、杀死APP重启后的断点续传等功能)
- Android更改状态栏颜色
- 测试用例设计白皮书
- 购物车实现功能1
- 自己写网页爬虫——网页分类抓取/采集并导入数据库
- synchronized(this)和synchronized(object)的理解
- Java系列笔记(3)