常用java 时间格式化:
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; } }View Code
标签:00,return,String,java,时间,Date,格式化,Calendar,calendar From: https://www.cnblogs.com/zxy-come-on/p/17519025.html