java时间工具类型,格式化时间,最近7天 月初 月末 季度 月度 时间格式化 等等
package com.tz.util; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 时间工具类 最近7天 月初 月末 季度 月度 时间格式化 等等…… * * @description 时间工具类 * @author: tz * @dtate: 2020/7/4 5:19 PM **/ public class DateTimeUtils { private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 今晚 00:00:00 * * @return Date 返回 Date 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static Date getTonightDate() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** * 今晚 00:00:00 * * @return String 返回 String 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static String getTonightToString() { return format.format(getTonightDate()); } /** * 昨晚 00:00:00 * * @return Date 返回 Date 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static Date getLastNightDate() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** * 昨晚 00:00:00 * * @return String 返回 String 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static String getLastNightToString() { return format.format(getLastNightDate()); } /** * 月初 00:00:00 * * @return Date 返回 Date 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static Date getStartMonthDate() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** * 月初 00:00:00 * * @return String 返回 String 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static String getStartMonthToString() { return format.format(getStartMonthDate()); } /** * 月末 00:00:00 * * @return Date 返回 Date 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static Date getEndMonthDate() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, 1); calendar.set(Calendar.DAY_OF_MONTH, 0); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** * 月末 00:00:00 * * @return String 返回 String 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static String getEndMonthToString() { return format.format(getEndMonthDate()); } /** * 当前时间 * * @return String 返回 String 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static String getCurrentTime() { return format.format(Calendar.getInstance().getTime()); } /** * 7天前 00:00:00 * * @return Date 返回 Date 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static Date getSevenDaysAgo() { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 7); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); return calendar.getTime(); } /** * 7天前 00:00:00 * * @return String 返回 String 类型时间 * @description * @time 2020/7/4 5:19 PM */ public static String getSevenDaysAgoToString() { return format.format(getSevenDaysAgo()); } /** * 某一季度开始时间 * * @param quarter 季度 一年四个季度 1 2 3 4 * @param year 年 例如 2020 * @return Date 返回 Date 类型时间 * @time 2020/7/4 5:19 PM */ public static Date getQuarterStartTime(int quarter, int year) { Calendar now = Calendar.getInstance(); int minDay = now.getActualMinimum(Calendar.DAY_OF_MONTH); if (quarter == 1) { now.set(year, 0, minDay, 00, 00, 00); } else if (quarter == 2) { now.set(year, 3, minDay, 00, 00, 00); } else if (quarter == 3) { now.set(year, 6, minDay, 00, 00, 00); } else if (quarter == 4) { now.set(year, 9, minDay, 00, 00, 00); } return now.getTime(); } /** * 某一季度开始时间 * * @param quarter 季度 一年四个季度 1 2 3 4 * @param year 年 例如 2020 * @return String 返回 String 类型时间 * @time 2020/7/4 5:19 PM */ public static String getQuarterStartToString(int quarter, int year) { return format.format(getQuarterStartTime(quarter, year)); } /** * 某一季度结束时间 * * @param year 年 例如 2020 * @param quarter 季度 一年四个季度 1 2 3 4 * @return Date 返回 Date 类型时间 * @time 2020/7/4 5:19 PM */ public static Date getQuarterEndTime(int year, int quarter) { Calendar now = Calendar.getInstance(); int maxDay = now.getActualMaximum(Calendar.DAY_OF_MONTH); if (quarter == 1) { now.set(year, 2, maxDay, 23, 59, 59); } else if (quarter == 2) { now.set(year, 5, maxDay, 23, 59, 59); } else if (quarter == 3) { now.set(year, 8, maxDay, 23, 59, 59); } else if (quarter == 4) { now.set(year, 11, maxDay, 23, 59, 59); } return now.getTime(); } /** * 某一季度结束时间 * * @param quarter 季度 一年四个季度 1 2 3 4 * @param year 年 例如 2020 * @return String 返回 String 类型时间 * @time 2020/7/4 5:19 PM */ public static String getQuarterEndToString(int year, int quarter) { return format.format(getQuarterEndTime(year, quarter)); } /** * 某年某月开始时间 * * @param year 年份 * @param month 月份 0-11 * @return Date 返回 Date 类型时间 * @time 2020/7/4 5:19 PM */ public static Date getMonthStartDate(int year, int month) { Calendar now = Calendar.getInstance(); int minDay = now.getActualMinimum(Calendar.DAY_OF_MONTH); now.set(year, month, minDay, 00, 00, 00); return now.getTime(); } /** * 某年某月开始时间 * * @param year 年份 * @param month 月份 0-11 * @return String 返回 String 类型时间 * @time 2020/7/4 5:19 PM */ public static String getMonthStarToString(int year, int month) { return format.format(getMonthStartDate(year, month)); } /** * 某年某月结束时间 * * @param year 年份 * @param month 月份 0-11 * @return Date 返回 Date 类型时间 * @time 2020/7/4 5:19 PM */ public static Date getMonthEndDate(int year, int month) { Calendar now = Calendar.getInstance(); int maxDay = now.getActualMaximum(Calendar.DAY_OF_MONTH); now.set(year, month, maxDay, 23, 59, 59); return now.getTime(); } /** * 某年某月结束时间 * * @param year 年份 * @param month 月份 0-11 * @return String 返回 String 类型时间 * @time 2020/7/4 5:19 PM */ public static String getMonthEndToString(int year, int month) { return format.format(getMonthEndDate(year, month)); } /** * 字符串格式化 时间 * * @param time * @return java.util.Date * @time 2020/6/2 5:27 PM */ public static Date stringDateFormat(String time) { Date date = null; try { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); date = format.parse(time); } catch (Exception e) { e.printStackTrace(); } return date; } }
哇!又赚了一天人民币
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库