当前位置:网站首页>日期工具类(不定时更新)
日期工具类(不定时更新)
2022-07-02 17:25:00 【唯空城】
- 常见的日期操作工具类
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
/**
* 时间工具类
* @author sed
* @date 2022/6/12 11:39
*/
public class DateUtil {
/**
* 获得指定时间的时间戳
* @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;
}
/**
* 取得当前系统时间戳
* @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;
}
/**
* 日期格式字符串转日期
* @param recoure 日期格式字符串
* @param format 日期格式
* <yyyy-MM-dd> <yyyy-MM-dd HH:mm:ss> <yyyy-MM-dd 00:00:00> <yyyy年MM月dd日 HH时mm分>
* @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");
}
}
/**
* 日期转日期格式字符串
* @param date 日期
* @param format 日期格式
* <yyyy-MM-dd> <yyyy-MM-dd HH:mm:ss> <yyyy-MM-dd 00:00:00> <yyyy年MM月dd日 HH时mm分>
* @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");
}
}
/** 比较两个时间相差分钟 */
public static double calcTimeMinute(Date date1, Date date2)
{
double minute = 0;
double millisecond = date2.getTime() - date1.getTime();
minute = millisecond / (60 * 1000);
return minute;
}
/**
* 获取指定时间 之前或之后 几分钟的时间
* @param startTime 指定时间
* @param minute 分钟
* @param type -1 之前的时间 ,1 之后的时间
* @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;
}
/** 时间增加分钟 **/
public static Date addTimeMinute(Date date, Integer min)
{
return new Date(date.getTime() + min * 60000);
}
/** 时间减少分钟 **/
public static Date reduceTimeMinute(Date date, Integer min)
{
return new Date(date.getTime() - min * 60000);
}
/** 比较两个时间大小 **/
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比dateTwo大
result = 1;
}else if(result < 0){
// dateOne比dateTwo小
result = -1;
}else{
// 相等
result = 0 ;
}
return result ;
}
/** 获取昨天的日期格式 **/
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;
}
}
边栏推荐
- 呆错图床系统源码图片CDN加速与破J防盗链功能
- R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
- Concepts and differences of PR curve and ROC curve
- Simulateur nightGod + application de test de capture de paquets Fiddler
- Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
- Web实时通信技术之Websocket
- 在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天
- Use MNIST in tensorflow 2_ 784 data set for handwritten digit recognition
- Eliminate the yellow alarm light on IBM p750 small computer [easy to understand]
- R语言使用epiDisplay包的lrtest函数对多个glm模型(logisti回归)执行似然比检验(Likelihood ratio test)对比两个模型的性能是否有差异、广义线性模型的似然比检
猜你喜欢

UML class diagram

Web版3D可视化工具,程序员应该知道的97件事,AI前沿论文 | 资讯日报 #2022.07.01

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

Google's official response: we have not given up tensorflow and will develop side by side with Jax in the future

Redis(7)----数据库与过期键

谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展

开源物联网平台ThingsBoard的安装

Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比

医院在线问诊源码 医院视频问诊源码 医院小程序源码

Leetcode interview question 17.01 Addition without plus sign
随机推荐
[Yugong series] July 2022 go teaching course 001 introduction to go language premise
options should NOT have additional properties
[daily question] the next day
新加坡暑假旅遊攻略:一天玩轉新加坡聖淘沙島
Eliminate the yellow alarm light on IBM p750 small computer [easy to understand]
R语言使用epiDisplay包的cox.display函数获取cox回归模型汇总统计信息(风险率HR、调整风险率及其置信区间、模型系数的t检验的p值、Wald检验的p值和似然比检验的p值)、汇总统计
产品经理应具备的能力
R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
Leetcode(81)——搜索旋转排序数组 II
Redis(7)----数据库与过期键
【每日一题】第一天
R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
第一次去曼谷旅游怎么玩?这份省钱攻略请收好
Deep neural network Summary
Leetcode interview question 17.04 Vanishing numbers
【JVM调优实战100例】01——JVM的介绍与程序计数器
The difference between promise and observable
SLC、MLC、TLC 和 QLC NAND SSD 之间的区别:哪个更好?
Radian to angle, angle to radian in MATLAB
Crypto usage in nodejs