当前位置:网站首页>日期工具类(不定时更新)
日期工具类(不定时更新)
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;
}
}
边栏推荐
- 第一次去曼谷旅游怎么玩?这份省钱攻略请收好
- How to use PS to extract image color and analyze color matching
- R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
- R语言dplyr包na_if函数把向量数值中的控制转化为缺失值NA、按照映射规则把指定内容转化为缺失值NA
- The student Tiktok publicized that his alma mater was roast about "reducing the seal of enrollment". Netizen: hahahahahahahaha
- 快速排序基本思路(通俗易懂+例子)「建议收藏」
- 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
- options should NOT have additional properties
- Chain game system development (unity3d chain game development details) - chain game development mature technology source code
- R language uses the lsnofunction function function of epidisplay package to list all objects in the current space, except user-defined function objects
猜你喜欢

夜神模拟器+Fiddler抓包测试App

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

Stratégie touristique d'été de Singapour: un jour pour visiter l'île de San taosha à Singapour

UML class diagram

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

Leetcode(81)——搜索旋转排序数组 II

UE4 用spline畫正圓

夜神模擬器+Fiddler抓包測試App

揭秘得物客服IM全链路通信过程

阿里三面被面试官狂问Redis,简历上再也不敢写'精通'了
随机推荐
R language ggplot2 visual Facet: gganimate package is based on Transition_ Time function to create dynamic scatter animation (GIF)
Simulateur nightGod + application de test de capture de paquets Fiddler
R language ggplot2 visualization: visualize the line chart and add customized X-axis label information to the line chart using labs function
深度学习数学基础
Progress-进度条
The official docker image running container in version 1.5.1 can be set to use MySQL 8 driver?
彻底搞懂基于Open3D的点云处理教程!
Rte11 interrupt decoupling function
Websocket of Web real-time communication technology
@Component 拿不到dao层
医院在线问诊源码 医院视频问诊源码 医院小程序源码
How to write controller layer code gracefully?
一款简约PHP个人发卡程序V4.0版本
怎么用ps提取图片颜色分析色彩搭配
Leetcode (81) -- search rotation sort array II
材质UV遮罩的技巧
Web version 3D visualization tool, 97 things programmers should know, AI frontier paper | information daily # 2022.07.01
阿里三面被面试官狂问Redis,简历上再也不敢写'精通'了
Typical application of "stack" - expression evaluation (implemented in C language)
在支付宝账户上买基金安全吗