当前位置:网站首页>Time tools
Time tools
2022-07-07 19:39:00 【Scarecrow 0.0】
public class DateUtils {
public static final Date MAX_DATE = new Date(4102243199000L);
private DateUtils() {
throw new UnsupportedOperationException(this.getClass().getName() + " is a tool class, can't be created instance!");
}
public static String convertTimeToString(long longTime) {
return convertTimeToString(longTime, "yyyy-MM-dd HH:mm:ss");
}
public static String convertTimeToString(long longTime, String format) {
try {
Timestamp t = new Timestamp(longTime * 1000L);
SimpleDateFormat sDateFormat = new SimpleDateFormat(format);
return sDateFormat.format(t);
} catch (Exception var5) {
throw new RuntimeException("Can't format the time by format[" + format + "]!");
}
}
public static long convertTimeToLong(String dateTime) {
return convertTimeToLong(dateTime, "yyyy-MM-dd HH:mm:ss");
}
public static long convertTimeToLong(String dateTime, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
long date = 0L;
try {
Date d = sdf.parse(dateTime);
date = d.getTime() / 1000L;
return date;
} catch (Exception var6) {
throw new RuntimeException("Can't format the time by format[" + format + "]!");
}
}
public static long getCurrentLongTime() {
return System.currentTimeMillis() / 1000L;
}
public static String getCurrentTime() {
return getCurrentTime("yyyy-MM-dd HH:mm:ss");
}
public static String getCurrentTime(String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
} catch (Exception var2) {
throw new RuntimeException("Can't format the time by format[" + format + "]!");
}
}
public static long getDayBetweenDateAndDate(String startDate, String endDate) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0L;
try {
Date date = myFormatter.parse(endDate);
Date mydate = myFormatter.parse(startDate);
day = (date.getTime() - mydate.getTime()) / 86400000L;
return day;
} catch (Exception var7) {
throw new RuntimeException("Time format[" + startDate + "][" + endDate + "] is error ! format must be 'yyyy-MM-dd'!");
}
}
public static int getWeekday(String dateTime) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = myFormatter.parse(dateTime);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(7) - 1 < 0 ? 0 : cal.get(7) - 1;
} catch (ParseException var4) {
throw new RuntimeException("Time format[" + dateTime + "] is error ! format must be 'yyyy-MM-dd'!");
}
}
public static String getPreWeekFirstDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus - 7);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getPreWeekLastDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus - 1);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getNextWeekFirstDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + 7);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getNextWeekLastDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + 7 + 6);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getWeekFirstDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getWeekLastDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + 6);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getPreMonthFirstDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(5, 1);
lastDate.add(2, -1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getPreMonthLastDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(2, -1);
lastDate.set(5, 1);
lastDate.roll(5, -1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getMonthFirstDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(5, 1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getMonthLastDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(5, 1);
lastDate.add(2, 1);
lastDate.add(5, -1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getNextMonthFirstDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(2, 1);
lastDate.set(5, 1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getNextMonthLastDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(2, 1);
lastDate.set(5, 1);
lastDate.roll(5, -1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getBeforeAfterDate(String dateTime, int day) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date oldDate = null;
try {
df.setLenient(false);
oldDate = new Date(df.parse(dateTime).getTime());
} catch (ParseException var11) {
throw new RuntimeException("Time format[" + dateTime + "] is error ! format must be 'yyyy-MM-dd'!");
}
Calendar cal = new GregorianCalendar();
cal.setTime(oldDate);
int Year = cal.get(1);
int Month = cal.get(2);
int Day = cal.get(5);
int NewDay = Day + day;
cal.set(1, Year);
cal.set(2, Month);
cal.set(5, NewDay);
Date date = new Date(cal.getTimeInMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
}
边栏推荐
- [tpm2.0 principle and Application guide] Chapter 9, 10 and 11
- Kirin Xin'an cloud platform is newly upgraded!
- 一张图深入的理解FP/FN/Precision/Recall
- CMD command enters MySQL times service name or command error (fool teaching)
- 网易云信参与中国信通院《实时音视频服务(RTC)基础能力要求及评估方法》标准编制...
- How to buy stocks on your mobile phone and open an account? Is it safe to open an account
- 9 原子操作类之18罗汉增强
- 2022.07.05
- 浏览积分设置的目的
- Policy mode - unity
猜你喜欢
How to share the same storage among multiple kubernetes clusters
爬虫实战(七):爬王者英雄图片
How to estimate the value of "not selling pens" Chenguang?
8 CAS
Micro service remote debug, nocalhost + rainbow micro service development second bullet
转置卷积理论解释(输入输出大小分析)
ES6 note 1
AD域组策略管理
Jerry's headphones with the same channel are not allowed to pair [article]
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
随机推荐
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
【牛客网刷题系列 之 Verilog进阶挑战】~ 多bit MUX同步器
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
Kirin Xin'an joins Ningxia commercial cipher Association
反爬虫的重点:识别爬虫
位运算介绍
R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
R语言fpc包的dbscan函数对数据进行密度聚类分析、查看所有样本的聚类标签、table函数计算聚类簇标签与实际标签构成的二维列联表
Classification and application of enterprise MES Manufacturing Execution System
ASP.NET体育馆综合会员管理系统源码,免费分享
LeetCode1051(C#)
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
Former richest man, addicted to farming
Where does brain hole come from? New research from the University of California: creative people's neural connections will "take shortcuts"
Uvalive – 4621 CAV greed + analysis "suggestions collection"
“本真”是什么意思
Hongmeng smart home [1.0]
我的创作纪念日
The research group of the Hunan Organizing Committee of the 24th China Association for science and technology visited Kirin Xin'an