当前位置:网站首页>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;
}
}
边栏推荐
- Fastdfs installation
- R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
- How to copy and paste interlaced in Excel
- Competence of product manager
- Page title component
- Distance measurement - Jaccard distance
- Golang并发编程——goroutine、channel、sync
- 从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式
- STM32G0 USB DFU 升级校验出错-2
- C文件输入操作
猜你喜欢

Industrial software lecture - core technology analysis of 3D CAD design software - the second lecture of the Forum

使用CLion编译OGLPG-9th-Edition源码

Hongmeng's fourth learning
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day

第一次去曼谷旅游怎么玩?这份省钱攻略请收好

彻底搞懂基于Open3D的点云处理教程!

【JVM调优实战100例】01——JVM的介绍与程序计数器

Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base

Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB

juypter notebook 修改默认打开文件夹以及默认浏览器
随机推荐
How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future
juypter notebook 修改默认打开文件夹以及默认浏览器
高频面试题
Kubernetes three open interfaces first sight
M2DGR:多源多场景 地面机器人SLAM数据集(ICRA 2022 )
Slam | how to align timestamps?
鸿蒙第四次学习
第一次去曼谷旅游怎么玩?这份省钱攻略请收好
Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
材质UV遮罩的技巧
Talk about the design of red envelope activities in e-commerce system
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
Have you stepped on the nine common pits in the e-commerce system?
[fluent] dart data type (VaR data type | object data type)
Emmet基础语法
Page title component
Yolov3 trains its own data set to generate train txt
reduce--遍历元素计算 具体的计算公式需要传入 结合BigDecimal
从list转化成map的时候,如果根据某一属性可能会导致key重复而异常,可以设置处理这种重复的方式