本文主要是介绍常用工具类规整-DateUtil,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.live.test.api.core.date.ju;import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;/*** 日期工具类 基于 java.util.Calendar* * @author live* @2020年2月19日 @下午*/
public class DateUtil {/*** yyyy_MM_dd*/public static final String FORMAT_yyyy_MM_dd = "yyyy-MM-dd";/*** yyyy-MM*/public static final String FORMAT_yyyy_MM = "yyyy-MM";/*** 1秒 的毫秒数*/public static final long MILLISECOND_OF_SECOND = 1000;/*** 1分钟 的毫秒数*/public static final long MILLISECOND_OF_MINUTE = MILLISECOND_OF_SECOND * 60;/*** 1小时 的毫秒数*/public static final long MILLISECOND_OF_HOUR = MILLISECOND_OF_MINUTE * 60;/*** 1天 的毫秒数*/public static final long MILLISECOND_OF_DAY = MILLISECOND_OF_HOUR * 24;/*** 获取当前时刻 的时间戳* * @return 毫秒*/public static long getNowMillis() {return System.currentTimeMillis();}/*** 获取当前时刻 距离 次日0点0分0秒 的毫秒数* * @return 毫秒*/public static long getNowToZeroMillis() {return getZeroMillis() - getNowMillis();}/*** 获取 次日0点0分0秒 的时间戳* * @return 毫秒*/public static long getZeroMillis() {Calendar calendar = Calendar.getInstance();calendar.add(Calendar.DAY_OF_MONTH, 1);// 加1表示明天的0点calendar.set(Calendar.HOUR_OF_DAY, 0);// 24小时制calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MILLISECOND, 0);return calendar.getTimeInMillis();}/*** */public static String format = "yyyy-MM-dd HH:mm:ss";/*** 判断两个日期是否同日* * @param date1* @param date2* @return*/public static boolean isEqualDay(Date date1, Date date2) {Calendar calendar1 = Calendar.getInstance();calendar1.setTime(date1);Calendar calendar2 = Calendar.getInstance();calendar2.setTime(date2);int i = calendar1.get(Calendar.DAY_OF_YEAR);int i2 = calendar2.get(Calendar.DAY_OF_YEAR);return i == i2;}/*** 判断两个日期是否同周* * @param date1* @param date2* @return*/public static boolean isEqualWeek(Date date1, Date date2) {Calendar calendar1 = Calendar.getInstance();calendar1.setTime(date1);Calendar calendar2 = Calendar.getInstance();calendar2.setTime(date2);int week1 = calendar1.get(Calendar.WEEK_OF_MONTH);int week2 = calendar2.get(Calendar.WEEK_OF_MONTH);return isEqualMonth(date1, date2) && week1 == week2;}/*** 判断两个日期是否同月* * @param date1* @param date2* @return*/public static boolean isEqualMonth(Date date1, Date date2) {Calendar calendar1 = Calendar.getInstance();calendar1.setTime(date1);Calendar calendar2 = Calendar.getInstance();calendar2.setTime(date2);return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)&& calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH);}/*** 获取指定年月的第一天* * @param year* @param month* @return*/public static String getFirstDayOfMonth(int year, int month) {Calendar cal = Calendar.getInstance();cal.set(Calendar.YEAR, year);cal.set(Calendar.MONTH, month - 1);int firstDay = cal.getMinimum(Calendar.DATE);cal.set(Calendar.DAY_OF_MONTH, firstDay);SimpleDateFormat sdf = new SimpleDateFormat(format);return sdf.format(cal.getTime());}/*** 获取指定年月的最后一天* * @param year* @param month* @return*/public static String getLastDayOfMonth(int year, int month) {Calendar cal = Calendar.getInstance();cal.set(Calendar.YEAR, year);cal.set(Calendar.MONTH, month - 1);int lastDay = cal.getActualMaximum(Calendar.DATE);cal.set(Calendar.DAY_OF_MONTH, lastDay);SimpleDateFormat sdf = new SimpleDateFormat(format);return sdf.format(cal.getTime());}public static String coverToString(Date date, String format) {return new SimpleDateFormat(format).format(date);}/*** Date对象转为: 年月日* * @param date* @return*/public static String coverToDay(Date date) {return coverToString(date, FORMAT_yyyy_MM_dd);}/*** Date对象转为: 年月* * @param date* @return*/public static String coverToMonth(Date date) {return coverToString(date, FORMAT_yyyy_MM);}
}
代码地址:
github:https://github.com/sunxiaoning90/com_live_test/tree/master/com_live_test_javase/com_live_test_javase_core
这篇关于常用工具类规整-DateUtil的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!