2025-05-15 11:08:23 +08:00
|
|
|
|
package com.qxcm.moduleutil.utils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
|
|
|
|
public class TimeUtils {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取年
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getYear() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
return cd.get(Calendar.YEAR);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取月
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getMonth() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
return cd.get(Calendar.MONTH) + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getMonths() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
int month = cd.get(Calendar.MONTH) + 1;
|
|
|
|
|
|
if (month <= 9) {
|
|
|
|
|
|
return "0" + month;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return String.valueOf(month);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取日
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getDay() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
return cd.get(Calendar.DATE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getDays() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
int day = cd.get(Calendar.DATE);
|
|
|
|
|
|
if (day <= 9) {
|
|
|
|
|
|
return "0" + day;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return String.valueOf(day);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取时
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getHour() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
return cd.get(Calendar.HOUR);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getFormatterHour() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
int hour = cd.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
|
if (hour < 10) {
|
|
|
|
|
|
return "0" + hour;
|
|
|
|
|
|
}
|
|
|
|
|
|
return String.valueOf(hour);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取时
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getNewHour() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
return cd.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取分
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getMinute() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
return cd.get(Calendar.MINUTE);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取分
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getFormatterMinute() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
int minute = cd.get(Calendar.MINUTE);
|
|
|
|
|
|
if (minute < 10) {
|
|
|
|
|
|
return "0" + minute;
|
|
|
|
|
|
}
|
|
|
|
|
|
return String.valueOf(minute);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取秒
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getSeconds() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
return cd.get(Calendar.SECOND);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取秒
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getFormatterSeconds() {
|
|
|
|
|
|
Calendar cd = Calendar.getInstance();
|
|
|
|
|
|
int seconds = cd.get(Calendar.SECOND);
|
|
|
|
|
|
if (seconds < 10) {
|
|
|
|
|
|
return "0" + seconds;
|
|
|
|
|
|
}
|
|
|
|
|
|
return String.valueOf(seconds);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据某年份、某月份获取对应的月份天数
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param year 年份
|
|
|
|
|
|
* @param month 月份
|
|
|
|
|
|
* @return 月份的天数
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getDaysByYearMonth(int year, int month) {
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
|
|
calendar.set(Calendar.YEAR, year);
|
|
|
|
|
|
calendar.set(Calendar.MONTH, month - 1);
|
|
|
|
|
|
calendar.set(Calendar.DATE, 1);
|
|
|
|
|
|
calendar.roll(Calendar.DATE, -1);
|
|
|
|
|
|
int days = calendar.get(Calendar.DATE);
|
|
|
|
|
|
return days;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 日期字符串例如 2015-3-10 Num:需要减少的天数例如 7
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param day 日期字符串例如 2015-3-10
|
|
|
|
|
|
* @param Num 需要减少的天数例如 7
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getDateStr(String day, int Num) {
|
|
|
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
Date nowDate = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
nowDate = df.parse(day);
|
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
//如果需要向后计算日期 -改为+
|
|
|
|
|
|
Date newDate2 = new Date(nowDate.getTime() - (long) Num * 24 * 60 * 60 * 1000);
|
|
|
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
String dateOk = simpleDateFormat.format(newDate2);
|
|
|
|
|
|
return dateOk;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*获取系统时间 格式为:"yyyy/MM/dd "*/
|
|
|
|
|
|
public static String getCurrentDate() {
|
|
|
|
|
|
Date d = new Date();
|
|
|
|
|
|
SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
|
|
|
return sf.format(d);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*时间戳转换成字符窜*/
|
|
|
|
|
|
public static String getDateToString(long time) {
|
|
|
|
|
|
Date d = new Date(time);
|
|
|
|
|
|
SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
|
|
|
return sf.format(d);
|
|
|
|
|
|
}
|
2025-07-12 19:08:21 +08:00
|
|
|
|
public static String getDateToStringNoZ(long time) {
|
|
|
|
|
|
Date d = new Date(time);
|
|
|
|
|
|
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
|
|
|
return sf.format(d);
|
|
|
|
|
|
}
|
2025-05-15 11:08:23 +08:00
|
|
|
|
|
|
|
|
|
|
/*将字符串转为时间戳*/
|
|
|
|
|
|
public static long getStringToDate(String time) {
|
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
|
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
|
try {
|
|
|
|
|
|
date = sdf.parse(time);
|
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
return date.getTime();
|
|
|
|
|
|
}
|
|
|
|
|
|
//获取当前日期
|
|
|
|
|
|
public static String getCurrentDate2() {
|
|
|
|
|
|
Date d = new Date();
|
|
|
|
|
|
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
return sf.format(d);
|
|
|
|
|
|
}
|
2025-07-04 16:38:21 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 将毫秒数转换为 MM:ss 格式(如:03:45)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String formatDuration(long durationMs) {
|
|
|
|
|
|
long seconds = (durationMs / 1000) % 60;
|
|
|
|
|
|
long minutes = (durationMs / 1000 / 60) % 60;
|
2025-05-15 11:08:23 +08:00
|
|
|
|
|
2025-07-04 16:38:21 +08:00
|
|
|
|
return String.format("%02d:%02d", minutes, seconds);
|
|
|
|
|
|
}
|
2025-05-15 11:08:23 +08:00
|
|
|
|
}
|