当前位置:网站首页>Examples of time (calculation) total tools: start time and end time of this year, etc
Examples of time (calculation) total tools: start time and end time of this year, etc
2022-07-04 23:15:00 【pingzhuyan】
General tools of time operation
package com.aisce.common.utils;
/**
* @Author pzy
* @Description: TODO
* @Version 0.1.0
*/
import java.sql.Timestamp;
import java.util.*;
public class DateUtils {
// Get the start time of the day
public static Date getDayBegin() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}
// Get the end time of the day
public static Date getDayEnd() {
Calendar cal = new GregorianCalendar();
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
return cal.getTime();
}
// Get the start time of yesterday
public static Date getBeginDayOfYesterday() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
// Get the end time of yesterday
public static Date getEndDayOfYesterDay() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, -1);
return cal.getTime();
}
// Get the start time of tomorrow
public static Date getBeginDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayBegin());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
// Get the end time of tomorrow
public static Date getEndDayOfTomorrow() {
Calendar cal = new GregorianCalendar();
cal.setTime(getDayEnd());
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
// Get the start time of the week
public static Date getBeginDayOfWeek() {
Date date = new Date();
if (date == null) {
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
if (dayofweek == 1) {
dayofweek += 7;
}
cal.add(Calendar.DATE, 2 - dayofweek);
return getDayStartTime(cal.getTime());
}
// Get the end time of the week
public static Date getEndDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(getBeginDayOfWeek());
cal.add(Calendar.DAY_OF_WEEK, 6);
Date weekEndSta = cal.getTime();
return getDayEndTime(weekEndSta);
}
// Get the start time of this month
public static Date getBeginDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
return getDayStartTime(calendar.getTime());
}
// Get the end time of this month
public static Date getEndDayOfMonth() {
Calendar calendar = Calendar.getInstance();
calendar.set(getNowYear(), getNowMonth() - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(getNowYear(), getNowMonth() - 1, day);
return getDayEndTime(calendar.getTime());
}
// Get the start time of last month
public static Date getBeginDayOfLastMonth() {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendar.MONTH, -1);
calendar.set(calendar.DAY_OF_MONTH, 1);
return getDayStartTime(calendar.getTime());
}
// Get the end time of last month
public static Date getEndDayOfLastMonth() {
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(calendar.MONTH, -1);
int day = calendar.getActualMaximum(5);
calendar.set(calendar.DAY_OF_MONTH, day);
return getDayEndTime(calendar.getTime());
}
// Get the start time of the year
public static Date getBeginDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
// cal.set
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTime());
}
// obtain n The beginning time of years ago
public static Date getBeginDayOfLastYear(int n) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear() - n);
// cal.set
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
return getDayStartTime(cal.getTime());
}
// obtain n End time years ago
public static Date getEndDayOfLastYear(int n) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear() - n);
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DATE, 31);
return getDayEndTime(cal.getTime());
}
// Get the end time of the year
public static Date getEndDayOfYear() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, getNowYear());
cal.set(Calendar.MONTH, Calendar.DECEMBER);
cal.set(Calendar.DATE, 31);
return getDayEndTime(cal.getTime());
}
// Get the start time of a date
public static Timestamp getDayStartTime(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d)
calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0,
0, 0);
calendar.set(Calendar.MILLISECOND, 0);
return new Timestamp(calendar.getTimeInMillis());
}
// Get the end time of a date
public static Timestamp getDayEndTime(Date d) {
Calendar calendar = Calendar.getInstance();
if (null != d)
calendar.setTime(d);
calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23,
59, 59);
calendar.set(Calendar.MILLISECOND, 999);
return new Timestamp(calendar.getTimeInMillis());
}
// Get what year this year is
public static Integer getNowYear() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return Integer.valueOf(gc.get(1));
}
// Get which month this month is
public static int getNowMonth() {
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
return gc.get(2) + 1;
}
// The number of days obtained by subtracting two dates
public static int getDiffDays(Date beginDate, Date endDate) {
if (beginDate == null || endDate == null) {
throw new IllegalArgumentException("getDiffDays param is null!");
}
long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);
int days = new Long(diff).intValue();
return days;
}
// The number of milliseconds obtained by subtracting two dates
public static long dateDiff(Date beginDate, Date endDate) {
long date1ms = beginDate.getTime();
long date2ms = endDate.getTime();
return date2ms - date1ms;
}
// Get the largest of the two dates
public static Date max(Date beginDate, Date endDate) {
if (beginDate == null) {
return endDate;
}
if (endDate == null) {
return beginDate;
}
if (beginDate.after(endDate)) {
return beginDate;
}
return endDate;
}
// Get the smallest of the two dates
public static Date min(Date beginDate, Date endDate) {
if (beginDate == null) {
return endDate;
}
if (endDate == null) {
return beginDate;
}
if (beginDate.after(endDate)) {
return endDate;
}
return beginDate;
}
// Return to the first month of the quarter of a month
public static Date getFirstSeasonDate(Date date) {
final int[] SEASON = {1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int sean = SEASON[cal.get(Calendar.MONTH)];
cal.set(Calendar.MONTH, sean * 3 - 3);
return cal.getTime();
}
// Returns the date of the next few days of a date
public static Date getNextDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
return cal.getTime();
}
// Returns the date several days before a date
public static Date getFrontDay(Date date, int i) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i);
return cal.getTime();
}
// Get the slice date set by day from a certain month of a certain year to a certain month of a certain year ( Date set of interval days )
public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) {
List list = new ArrayList();
if (beginYear == endYear) {
for (int j = beginMonth; j <= endMonth; j++) {
list.add(getTimeList(beginYear, j, k));
}
} else {
{
for (int j = beginMonth; j < 12; j++) {
list.add(getTimeList(beginYear, j, k));
}
for (int i = beginYear + 1; i < endYear; i++) {
for (int j = 0; j < 12; j++) {
list.add(getTimeList(i, j, k));
}
}
for (int j = 0; j <= endMonth; j++) {
list.add(getTimeList(endYear, j, k));
}
}
}
return list;
}
// Get a collection of sliced dates by day in a certain month of a certain year ( How many days apart is a date set in a month )
public static List getTimeList(int beginYear, int beginMonth, int k) {
List list = new ArrayList();
Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1);
int max = begincal.getActualMaximum(Calendar.DATE);
for (int i = 1; i < max; i = i + k) {
list.add(begincal.getTime());
begincal.add(Calendar.DATE, k);
}
begincal = new GregorianCalendar(beginYear, beginMonth, max);
list.add(begincal.getTime());
return list;
}
// Get the date of the first day of a month in a year
public static Date getStartMonthDate(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1, 0, 0, 0);
return calendar.getTime();
}
// Get the last day date of a month in a year
public static Date getEndMonthDate(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(year, month - 1, day, 23, 59, 59);
return calendar.getTime();
}
}Test case 1 :
// test Case study :
public static void main(String[] args) {
/**
* Test one : Get the start of the year End time and A year ago. Starting time And the end time a year ago
*/
Date beginDayOfYear = getBeginDayOfYear();
Date endDayOfYear = getEndDayOfYear();
System.out.println(beginDayOfYear);//2022-01-01 00:00:00.0
System.out.println(endDayOfYear);//2022-12-31 23:59:59.999
Date beginDayOfLastYear = getBeginDayOfLastYear(1);// It started a year ago
Date endDayOfLastYear = getEndDayOfLastYear(1);// It ended a year ago
System.out.println(beginDayOfLastYear);//2021-01-01 00:00:00.0
System.out.println(endDayOfLastYear);//2021-12-31 23:59:59.999
}边栏推荐
- OSEK标准ISO_17356汇总介绍
- [try to hack] wide byte injection
- [machine learning] handwritten digit recognition
- Phpcms paid reading function Alipay payment
- Redis:Redis消息的发布与订阅(了解)
- 数据库基础知识
- [Jianzhi offer] 6-10 questions
- HMS core machine learning service
- phpcms付费阅读功能支付宝支付
- One of the commonly used technical indicators, reading boll Bollinger line indicators
猜你喜欢
随机推荐
Google collab trample pit
为什么信息图会帮助你的SEO
Basic use and upgrade of Android native database
字体设计符号组合多功能微信小程序源码
Redis introduction complete tutorial: detailed explanation of ordered collection
[graph theory] topological sorting
Excel 快捷键-随时补充
ECS settings SSH key login
【剑指Offer】6-10题
Editplus-- usage -- shortcut key / configuration / background color / font size
Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
Photoshop批量给不同的图片添加不同的编号
Redis introduction complete tutorial: slow query analysis
C语言快速解决反转链表
Set up a website with a sense of ceremony, and post it to 1/2 of the public network through the intranet
[Jianzhi offer] 6-10 questions
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
ETCD数据库源码分析——处理Entry记录简要流程
A complete tutorial for getting started with redis: understanding and using APIs
A complete tutorial for getting started with redis: transactions and Lua








