当前位置:网站首页>日期工具类(不定时更新)
日期工具类(不定时更新)
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;
}
}
边栏推荐
- Nm01 function overview and API definition of nm module independent of bus protocol
- Hongmeng's fourth learning
- PR曲线和ROC曲线概念及其区别
- R语言ggplot2可视化分面图(facet):gganimate包基于transition_time函数创建动态散点图动画(gif)
- UML 类图
- Redis (7) -- database and expiration key
- AI开发调试系列第二弹:多机分布式调测探索之旅
- 彻底搞懂基于Open3D的点云处理教程!
- Leetcode (154) -- find the minimum value II in the rotation sort array
- 学生抖音宣传母校被吐槽“招生减章”,网友:哈哈哈哈哈哈
猜你喜欢
A simple PHP personal card issuing program v4.0
The text editor hopes to mark the wrong sentences in red, and the text editor uses markdown
Web版3D可视化工具,程序员应该知道的97件事,AI前沿论文 | 资讯日报 #2022.07.01
拦截器与过滤器的区别
Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour
Industrial software lecture - core technology analysis of 3D CAD design software - the second lecture of the Forum
什么是云原生?这回终于能搞明白了!
CDN acceleration and breaking J anti-theft chain function
Tips for material UV masking
文字编辑器 希望有错误的句子用红色标红,文字编辑器用了markdown
随机推荐
AI开发调试系列第二弹:多机分布式调测探索之旅
医院在线问诊源码 医院视频问诊源码 医院小程序源码
IPtable port redirection masquerade[easy to understand]
Distance measurement - Jaccard distance
【JVM调优实战100例】01——JVM的介绍与程序计数器
@Component 拿不到dao层
The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
新加坡暑假旅游攻略:一天玩转新加坡圣淘沙岛
Ali was wildly asked by the interviewer on three sides. Redis dared not write 'proficient' on his resume anymore
SQL training 2
PR曲线和ROC曲线概念及其区别
R语言dplyr包filter函数筛选dataframe数据、如果需要筛选的数据列(变量)名称中包含引号则需要使用!!sym语法处理、否则因为无法处理引号筛选不到任何数据
Looking for innocence in New York -- a beautiful day at the discovery center of Legoland, New Jersey
After 22 years in office, the father of PowerShell will leave Microsoft: he was demoted by Microsoft for developing PowerShell
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
徹底搞懂基於Open3D的點雲處理教程!
Deep neural network Summary
M2DGR:多源多场景 地面机器人SLAM数据集(ICRA 2022 )
The second bullet of AI development and debugging series: the exploration journey of multi machine distributed debugging
Chain game system development (unity3d chain game development details) - chain game development mature technology source code