当前位置:网站首页>日期工具类(不定时更新)
日期工具类(不定时更新)
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;
}
}
边栏推荐
- Slam | how to align timestamps?
- The difference between SLC, MLC, TLC and QLC NAND SSD: which is better?
- 【JVM调优实战100例】01——JVM的介绍与程序计数器
- How to delete the border of links in IE? [repeat] - how to remove borders around links in IE? [duplicate]
- UE4 用spline畫正圓
- 问题包含哪些环节
- options should NOT have additional properties
- 全链路数字化转型下,零售企业如何打开第二增长曲线
- A simple PHP personal card issuing program v4.0
- [daily question] first day
猜你喜欢

Uncover the whole link communication process of dewu customer service im

在纽约寻找童真——新泽西州乐高乐园探索中心的美好一天

故障排查:kubectl报错ValidationError: unknown field \u00a0

Leetcode interview question 17.01 Addition without plus sign
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day

What is cloud primordial? This time, I can finally understand!

徹底搞懂基於Open3D的點雲處理教程!

科技公司不同人对Bug的反应 | 每日趣闻

【每日一题】第二天

UML 类图
随机推荐
Nm01 function overview and API definition of nm module independent of bus protocol
页面标题组件
The official docker image running container in version 1.5.1 can be set to use MySQL 8 driver?
Leetcode interview question 16.15 Abacus wonderful calculation
cJSON 使用详解
Redis (7) -- database and expiration key
Nm02 nm module call sequence diagram and code interpretation independent of bus protocol
谷歌官方回应:我们没有放弃TensorFlow,未来与JAX并肩发展
【每日一题】第一天
Leetcode interview question 16.11 Diving board
UE4 用spline畫正圓
Leetcode interview question 17.04 Vanishing numbers
R语言使用epiDisplay包的lsNoFunction函数列出当前空间中的所有对象、除了用户自定义的函数对象
[daily question] first day
Leetcode (154) -- find the minimum value II in the rotation sort array
M2dgr: slam data set of multi-source and multi scene ground robot (ICRA 2022)
“栈”的典型应用—表达式求值(C语言实现)
Leetcode(154)——寻找旋转排序数组中的最小值 II
SAP S/4HANA OData Mock Service 介绍
Ali was wildly asked by the interviewer on three sides. Redis dared not write 'proficient' on his resume anymore