当前位置:网站首页>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
}
边栏推荐
- CTF競賽題解之stm32逆向入門
- JS 3D explosive fragment image switching JS special effect
- Redis入门完整教程:键管理
- 机器学习在房屋价格预测上的应用
- Complete tutorial for getting started with redis: bitmaps
- 实战模拟│JWT 登录认证
- Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
- A complete tutorial for getting started with redis: getting to know redis for the first time
- CTF竞赛题解之stm32逆向入门
- Redis入门完整教程:哈希说明
猜你喜欢
Redis入门完整教程:哈希说明
Redis introduction complete tutorial: detailed explanation of ordered collection
PS style JS webpage graffiti board plug-in
CTF竞赛题解之stm32逆向入门
S32 Design Studio for ARM 2.2 快速入门
[roommate learned to use Bi report data processing in the time of King glory in one game]
实战模拟│JWT 登录认证
Redis入门完整教程:Redis Shell
壁仞科技研究院前沿技术文章精选
P2181 对角线和P1030 [NOIP2001 普及组] 求先序排列
随机推荐
Stm32 Reverse Introduction to CTF Competition Interpretation
位运算符讲解
UML图记忆技巧
Complete tutorial for getting started with redis: bitmaps
【taichi】用最少的修改将太极的pbf2d(基于位置的流体模拟)改为pbf3d
【ODX Studio編輯PDX】-0.2-如何對比Compare兩個PDX/ODX文件
Redis getting started complete tutorial: hash description
HMS core unified scanning service
Redis démarrer le tutoriel complet: Pipeline
[graph theory] topological sorting
OSEK标准ISO_17356汇总介绍
S32 Design Studio for ARM 2.2 快速入门
ECS settings SSH key login
微信公众号解决从自定义菜单进入的缓存问题
How to choose a securities company? Is it safe to open an account on your mobile phone
推荐收藏:跨云数据仓库(data warehouse)环境搭建,这货特别干!
[ODX studio edit PDX] - 0.2-how to compare two pdx/odx files of compare
Redis getting started complete tutorial: publish and subscribe
Summary of wechat applet display style knowledge points
该如何去选择证券公司,手机上开户安不安全