当前位置:网站首页>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;
}
}
边栏推荐
- How to enable the run dashboard function of idea
- The second bullet of AI development and debugging series: the exploration journey of multi machine distributed debugging
- Thoroughly understand the point cloud processing tutorial based on open3d!
- SLAM|如何时间戳对齐?
- R语言dplyr包rowwise函数、mutate函数计算dataframe数据中多个数据列在每行的最大值、并生成行最大值对应的数据列(row maximum)
- Talk about the design of red envelope activities in e-commerce system
- Golang concurrent programming goroutine, channel, sync
- 27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi
- ICDE 2023|TKDE Poster Session(CFP)
- M2DGR:多源多场景 地面机器人SLAM数据集(ICRA 2022 )
猜你喜欢
[daily question] first day
Compile oglpg-9th-edition source code with clion
深度学习数学基础
Excel如何进行隔行复制粘贴
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
学生抖音宣传母校被吐槽“招生减章”,网友:哈哈哈哈哈哈
Troubleshooting: kubectl reports an error validationerror: unknown field \u00a0
Novice must see, click two buttons to switch to different content
LightGroupButton* sender = static_cast<LightGroupButton*>(QObject::sender());
Thoroughly understand the point cloud processing tutorial based on open3d!
随机推荐
R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
The second bullet of AI development and debugging series: the exploration journey of multi machine distributed debugging
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
Leetcode (154) -- find the minimum value II in the rotation sort array
Mini Golf Course: a good place for leisure and tourism in London
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
【每日一题】第二天
ICDE 2023|TKDE Poster Session(CFP)
Deep learning mathematics foundation
Markdown基础语法
深度学习数学基础
日期工具类(不定时更新)
[Yugong series] July 2022 go teaching course 001 introduction to go language premise
yolov3 训练自己的数据集之生成train.txt
产品经理应具备的能力
开发固定资产管理系统,开发固定资产管理系统用什么语音
Kubernetes three open interfaces first sight
Mysql高级篇学习总结7:Mysql数据结构-Hash索引、AVL树、B树、B+树的对比
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计