当前位置:网站首页>Date tool class (updated from time to time)
Date tool class (updated from time to time)
2022-07-02 19:09:00 【Only empty city】
- Common date operation tool classes
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* Time tools
* @author sed
* @date 2022/6/12 11:39
*/
public class DateUtil {
/**
* Get the time stamp of the specified time
* @param data
* @return
*/
public static int getTimeStampByDate(String data,String pattern){
Date date = format(data, pattern);
Long tm = date.getTime()/1000;
return tm.intValue();
}
private static Date format(String str, String pattern) {
DateFormat formatter = new SimpleDateFormat(pattern, Locale.ENGLISH);
Date date = null;
try {
date = (Date) formatter.parse(str);
} catch (ParseException e) {
return null;
}
return date;
}
/**
* Get the current system timestamp
* @param pattern eg:yyyy-MM-dd HH:mm:ss,SSS
* @return
*/
public static int getSysTimeStamp(String pattern) {
return getTimeStampByDate(formatSysTime(new SimpleDateFormat(pattern)),pattern);
}
private static String formatSysTime(SimpleDateFormat format) {
String str = format.format(Calendar.getInstance().getTime());
return str;
}
/**
* Date format string to date
* @param recoure Date format string
* @param format Date format
* <yyyy-MM-dd> <yyyy-MM-dd HH:mm:ss> <yyyy-MM-dd 00:00:00> <yyyy year MM month dd Japan HH when mm branch >
* @return
*/
public static Date strToDate(String recoure, String format){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(format);
return dateFormat.parse(recoure);
} catch (ParseException e) {
throw new RuntimeException("parse date error");
}
}
/**
* Date to date format string
* @param date date
* @param format Date format
* <yyyy-MM-dd> <yyyy-MM-dd HH:mm:ss> <yyyy-MM-dd 00:00:00> <yyyy year MM month dd Japan HH when mm branch >
* @return
*/
public static String dateToStr(Date date, String format){
try {
SimpleDateFormat dateTimeFormat = new SimpleDateFormat(format);
return dateTimeFormat.format(date);
} catch (Exception e) {
throw new RuntimeException("parse date error");
}
}
/** Compare the difference between the two times by minutes */
public static double calcTimeMinute(Date date1, Date date2)
{
double minute = 0;
double millisecond = date2.getTime() - date1.getTime();
minute = millisecond / (60 * 1000);
return minute;
}
/**
* Get the specified time Before or after A few minutes
* @param startTime Specify time
* @param minute minute
* @param type -1 Time before ,1 Time after
* @return
*/
public static String getTimeByMinute(String startTime,int minute, String type ) {
//String startTime = "2018-05-10 11:10:50";
Date format = format(startTime, "yyyy-MM-dd HH:mm:ss");
long time = format.getTime();
if(type.equals("-1")){
time = time - (minute * 60 * 1000);
}else{
time = time + (minute * 60 * 1000);
}
String resultTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);
return resultTime;
}
/** Increase time by minutes **/
public static Date addTimeMinute(Date date, Integer min)
{
return new Date(date.getTime() + min * 60000);
}
/** Reduce time by minutes **/
public static Date reduceTimeMinute(Date date, Integer min)
{
return new Date(date.getTime() - min * 60000);
}
/** Compare the two time scales **/
public static int compareTime(String dateOne, String dateTwo , String dateFormatType){
DateFormat df = new SimpleDateFormat(dateFormatType);
Calendar calendarStart = Calendar.getInstance();
Calendar calendarEnd = Calendar.getInstance();
try {
calendarStart.setTime(df.parse(dateOne));
calendarEnd.setTime(df.parse(dateTwo));
} catch (ParseException e) {
e.printStackTrace();
return 100;
}
int result = calendarStart.compareTo(calendarEnd);
if(result > 0){
// dateOne Than dateTwo Big
result = 1;
}else if(result < 0){
// dateOne Than dateTwo Small
result = -1;
}else{
// equal
result = 0 ;
}
return result ;
}
/** Get the date format of yesterday **/
public static String getYesterday(){
DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar=Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,-24);
String yesterdayDate = dateFormat.format(calendar.getTime());
return yesterdayDate;
}
}
边栏推荐
- Deep learning mathematics foundation
- 迷你高尔夫球场:伦敦休闲旅游好去处
- R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
- Industrial software lecture - core technology analysis of 3D CAD design software - the second lecture of the Forum
- R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
- 深度学习数学基础
- M2DGR:多源多场景 地面机器人SLAM数据集(ICRA 2022 )
- Yolov3 trains its own data set to generate train txt
- 开发固定资产管理系统,开发固定资产管理系统用什么语音
- Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
猜你喜欢
How can retail enterprises open the second growth curve under the full link digital transformation
How to clean up discarded PVs and their corresponding folders
使用CLion编译OGLPG-9th-Edition源码
Exness in-depth good article: dynamic series - Case Analysis of gold liquidity (V)
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
彻底搞懂基于Open3D的点云处理教程!
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
医院在线问诊源码 医院视频问诊源码 医院小程序源码
[test development] software testing - concept
【JVM调优实战100例】03——JVM堆调优四例
随机推荐
SLC、MLC、TLC 和 QLC NAND SSD 之间的区别:哪个更好?
2022软件工程期末考试 回忆版
Emmet基础语法
全链路数字化转型下,零售企业如何打开第二增长曲线
Comprendre complètement le tutoriel de traitement de Point Cloud basé sur open3d!
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
二进制操作
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
Websocket of Web real-time communication technology
开发固定资产管理系统,开发固定资产管理系统用什么语音
Golang concurrent programming goroutine, channel, sync
MySQL advanced learning summary 8: overview of InnoDB data storage structure page, internal structure of page, row format
【测试开发】一文带你了解什么是软件测试
C文件输入操作
SQL training 2
故障排查:kubectl报错ValidationError: unknown field \u00a0
options should NOT have additional properties
Markdown基础语法