当前位置:网站首页>Date processing tool class dateutils (tool class 1)
Date processing tool class dateutils (tool class 1)
2022-07-07 01:56:00 【Novice Zhang~】
/** * Date Toolset */
@UtilityClass
public class DateUtils {
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String YYYY_MM_DD = "yyyyMMdd";
public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String TIME_FORMAT = "HH:mm:ss";
/** * Get relative time * * @param date * @param amount * @return */
public static String getRelativeTime(Date date, int amount) {
String addDate = DateUtils.getRelativeTime(date, amount);
return addDate;
}
/** * Convert time format 0000-00-00 00:00:00 —> 0000-00-00 * * @param dateTimeStr Time to switch * @return */
public static String toDateStr(@NonNull String dateTimeStr) {
return dateTimeStr.split(" ")[0];
}
/** * Convert time format Date Time format —> String Time format 0000-00-00 * * @param dateTimeStr Time to switch * @return */
public static String toDateStr(@NonNull Date dateTimeStr) {
return DateUtils.toDateStr(dateTimeStr);
}
/** * Convert time format String Time format 0000-00-00 -> Date Time format * * @param dateTimeStr Time to switch * @return */
public static Date toDate(@NonNull String dateTimeStr) {
Date date = null;
try {
// date =
// DateUtils.toDate(
// dateTimeStr, DateFormatEnum.ISO_8601_EXTENDED_DATE_FORMAT.getPattern());
date = DateUtils.toDate(dateTimeStr);
} catch (Exception e) {
throw new RuntimeException(" Convert time format String Time format 0000-00-00 -> Date Time format abnormal ");
}
return date;
}
/** * Convert time format String Time format 0000-00-00 -> Date Time format * * @param dateTimeStr Time to switch * @return */
public static Date toDateTime(@NonNull String dateTimeStr) {
Date date = null;
try {
// date =
// DateUtils.toDate(
// dateTimeStr, DateFormatEnum.ISO_8601_EXTENDED_DATETIME_FORMAT.getPattern());
date = DateUtils.toDate(dateTimeStr);
} catch (Exception e) {
throw new RuntimeException(" Convert time format String Time format 0000-00-00 -> Date Time format abnormal ");
}
return date;
}
/** * Get all the time from the creation time to yesterday according to the creation time * * @param gmtCreate Creation time * @return All time */
public static List<String> getTimest(String gmtCreate) {
List<String> datesStr = Lists.newArrayList();
Date gmtCreateDate = toDate(gmtCreate);
Date yesterdayDate = toDate(DateUtils.getRelativeTime(new Date(), -1));
while (gmtCreateDate.getTime() <= yesterdayDate.getTime()) {
datesStr.add(toDateStr(gmtCreateDate));
gmtCreateDate = toDate(DateUtils.getRelativeTime(gmtCreateDate, 1));
}
return datesStr;
}
/** * date turn LocalDateTime * * @param date * @return */
public static LocalDateTime dateToLocalDateTime(Date date) {
if (ObjectUtils.isNull(date)) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/** * date turn LocalDateTime * * @param localDateTime * @return */
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
if (ObjectUtils.isNull(localDateTime)) {
return null;
}
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static final Long SHOPEE_TIMESTAMP_LIMIT = 4294967295L;
/** * solve Shopee Timestamp limitation * * @param timestamp * @return */
public static long resolveShopeeTimestamp(Long timestamp) {
if (timestamp >= SHOPEE_TIMESTAMP_LIMIT) {
return timestamp / 1000L;
}
return timestamp;
}
/** * Date to string Format customization * * @param date * @param f * @return */
public static String dateStr(Date date, String f) {
if (date == null) {
return "";
}
SimpleDateFormat format = new SimpleDateFormat(f);
String str = format.format(date);
return str;
}
/** * Get the remaining time of the day * Returns the second value */
public long getRemainingTime() {
//long milliSecondsLeftToday = 86400000 - org.apache.commons.lang.time.DateUtils.getFragmentInMilliseconds(Calendar.getInstance(), Calendar.DATE);
return 86400 - org.apache.commons.lang.time.DateUtils.getFragmentInSeconds(Calendar.getInstance(), Calendar.DATE);
}
/** * take date Turn into ISODate In order to mongo distinguish * * @param dateStr * @return */
public static Date dateToISODate(Date dateStr) {
Date parse = null;
try {
// Parse string time
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
parse = format.parse(format.format(dateStr));
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/** * If it is a string, it needs to be converted to date Call again dateToISODate() Method * @param strDate * @return */
public static Long strToDateLong(String strDate,String f) {
Date date = null;
try {
SimpleDateFormat formatter = new SimpleDateFormat(f);
date = formatter.parse(strDate);
return date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/** * Timestamp to time string * @param tiemsmap Time stamp * @return Converted String */
public static String timestampToStr(Long tiemsmap){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(tiemsmap);
}
/** * Custom date analysis format * @param tiemsmap Time stamp * @param r Converted time format for example : yyyy-MM-dd HH:mm:ss * @return Converted String */
public static String timestampToStr(Long tiemsmap,String r){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(r);
return simpleDateFormat.format(tiemsmap);
}
}
边栏推荐
- AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
- Reptile practice (VI): novel of climbing pen interesting Pavilion
- 454 Baidu Mianjing 1
- WCF Foundation
- ROS learning (XIX) robot slam function package cartographer
- AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
- 2022 system integration project management engineer examination knowledge point: Mobile Internet
- New job insights ~ leave the old and welcome the new~
- AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
- 百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
猜你喜欢

ROS学习(26)动态参数配置

ROS学习(23)action通信机制

sql中批量删除数据---实体中的集合

Modify the system time of Px4 flight control

一文带你走进【内存泄漏】

ROS学习(十九)机器人SLAM功能包——cartographer

ROS学习(21)机器人SLAM功能包——orbslam的安装与测试

JVM 内存模型

Can't you understand the code of linked list in C language? An article allows you to grasp the secondary pointer and deeply understand the various forms of parameter passing in the function parameter

盒子拉伸拉扯(左右模式)
随机推荐
编译命令行终端 swift
Curl command
dvajs的基础介绍及使用
JVM memory model
Golang foundation - data type
ROS learning (23) action communication mechanism
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
ROS学习(十九)机器人SLAM功能包——cartographer
Long press the button to execute the function
ROS learning (26) dynamic parameter configuration
长按按钮执行函数
爬虫实战(六):爬笔趣阁小说
DS-5/RVDS4.0变量初始化错误
JS ES5也可以创建常量?
Google released a security update to fix 0 days that have been used in chrome
When grep looks for a process, it ignores the grep process itself
The cradle of eternity
Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
LeetCode. Sword finger offer 62 The last remaining number in the circle
Make DIY welding smoke extractor with lighting