hz-zhhq-app-service/greenH5modul/.svn/pristine/91/9150dadac52773a70fdb7105f14...

263 lines
6.5 KiB
Plaintext
Raw Normal View History

2025-01-21 13:12:35 +08:00
package com.nationalelectric.greenH5.utils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DateTimeHelper {
public static String format(String timeLong, String defaut) {
try {
long tmpLong = Long.parseLong(timeLong);
Date d = new Date(tmpLong);
String timeStr = format(d, "yyyy-MM-dd HH:mm:ss");
return timeStr;
} catch (Exception e) {
return defaut;
}
}
public static String getNowDate() {
return format(new Date(), "yyyy-MM-dd HH:mm:ss");
}
public static String getNowDate1() {
return format(new Date(), "yyyy-MM-dd");
}
public static String getNowDate2() {
return format(new Date(), "yyyyMMdd");
}
public static String getNowDay() {
return format(new Date(), "yyyy-MM-dd");
}
public static String getNowDate3() {
return format(new Date(), "yyyy-MM-dd HH:mm:ss.SSSSSS");
}
public static void main(String[] args) {
}
/**
* 两个时间相差距离多少天多少小时多少分多少秒
*
* @param str1
* 时间参数 1 格式2016-06-22 18:21:20
* @param str2
* 时间参数 2 格式2016-06-22 17:21:20
* @return String 返回值为xx天xx小时xx分xx秒
*/
public static String getDistanceTime(String str1, String str2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date one;
Date two;
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
long ms = 0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
// ms = (diff - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 -
// min * 60 * 1000 - sec * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
return hour + "小时" + min + "分" + sec + "秒";
}
/**
* smdate时间参数 String类型 YYYY-MM-dd bdate时间参数 String类型 YYYY-DD-dd
* return返回整形Int数字类型,例如:1 可用于计算两个日期的相差天数
*/
public static int daysBetween(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600 * 24);
return Integer.parseInt(String.valueOf(between_days));
}
/**
* 用于计算两个时间的大小,是否相等 beginTime参数:开始时间 endTime 参数:结束时间 开始时间 < 结束时间 :返回0 开始时间 >
* 结束时间 :返回1 开始时间 = 结束时间 :返回2
*/
public static int isDaySize(String beginTime, String endTime) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date bt = sdf.parse(beginTime);
Date et = sdf.parse(endTime);
if (!bt.before(et) && !bt.after(et)) {
return 2;
}
if (bt.before(et)) {
return 0;
} else {
return 1;
}
}
public static String getNowDateZH() {
return format(new Date(), "yyyy年MM月dd日");
}
public static String getNowDateHMS() {
return format(new Date(), "yyyyMMddHHmmss");
}
public static String getNowDateHM() {
return format(new Date(), "yyyyMMddHHmm");
}
public static String format(Date d, String f) {
SimpleDateFormat sdf = new SimpleDateFormat(f, Locale.US);
return sdf.format(d);
}
public static boolean isToday(String datetime) {
String todayStr = format(new Date(), "yyyy-MM-dd");
if (datetime.contains(todayStr)) {
return true;
}
return false;
}
public static String getNowDateyms() {
return format(new Date(), "yyyy-MM-dd HH:mm:ss");
}
/**
* 获取当前时间的天
*
* @return
*/
public static String getNowCurrentDay() {
return format(new Date(), "dd");
}
/**
* 获取当前时间的年份
*
* @return
*/
public static String getNowYear() {
return format(new Date(), "yyyy");
}
/**
* 获取当前时间的年月
*
* @return
*/
public static String getNowYearMonth() {
return format(new Date(), "yyyyMM");
}
/**
* 获取当前月份
*
* @return
*/
public static String getNowMonths() {
return format(new Date(), "MM");
}
/**
* 获取当前时间的日
*
* @return
*/
public static String getDay() {
return format(new Date(), "dd");
}
// 返回的是字符串型的时间,输入的
// 是String day, int x
public static String addDateMinut(String day, int x) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
// 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等很随意的不过在主函数调用时要和输入的变
// 量day格式一致
Date date = null;
try {
date = format.parse(day);
} catch (Exception ex) {
ex.printStackTrace();
}
if (date == null)
return "";
System.out.println("front:" + format.format(date)); // 显示输入的日期
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MINUTE, x);// 24小时制
date = cal.getTime();
System.out.println("after:" + format.format(date)); // 显示更新后的日期
cal = null;
return format.format(date);
}
/**
* 获取当前时间戳
*
* @return
*/
public static String getStamp() {
int temp = (int) (System.currentTimeMillis() / 1000);
return temp + "";
}
/**
* 获取本周为今年的第几周
*
* @return
*/
public static String getWeek(){
Calendar calendar = Calendar.getInstance();
return String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
}
/**
* 日期解析为时间戳
*
* @param dateString
* @param format
* @return Long
* @description
* @author cwchen
* @date 2024/3/16 15:11
*/
public static Long convertDateStringToTimestamp(String dateString, String format){
Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
date = sdf.parse(dateString);
} catch (ParseException e) {
return null;
}
return date.getTime();
}
}