Files
yusheng-android/BaseModule/src/main/java/com/xscm/moduleutil/utils/TimeUtils.java
2025-12-18 17:10:38 +08:00

322 lines
9.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.xscm.moduleutil.utils;
import java.math.BigDecimal;
import java.math.RoundingMode;
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);
}
public static String getDateToStringNoZ(long time) {
Date d = new Date(time);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sf.format(d);
}
/*将字符串转为时间戳*/
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日hh时mm分ss秒SSS");
return sf.format(d);
}
/**
* 将毫秒数转换为 MM:ss 格式03:45
*/
public static String formatDuration(long durationMs) {
long seconds = (durationMs / 1000) % 60;
long minutes = (durationMs / 1000 / 60) % 60;
return String.format("%02d:%02d", minutes, seconds);
}
public static String formatDuration2(long durationMs) {
long totalSeconds = durationMs / 1000;
long days = totalSeconds / 86400;
long hours = (totalSeconds % 86400) / 3600;
StringBuilder sb = new StringBuilder();
if (days > 0) {
sb.append(days).append("");
}
if (hours > 0 || days > 0) {
sb.append(hours).append("小时 ");
}
// sb.append(String.format("%02d:%02d"));
return sb.toString().trim();
}
/**
* 格式化时长(毫秒),只返回天数部分。
* 例如:输入 90000000 (90,000秒即1天多),返回 "1天"
*
* @param durationMs 时长,单位为毫秒
* @return 格式化后的天数字符串,如 "1天",如果不足一天则返回空字符串 ""
*/
public static String formatDurationDaysOnly(long durationMs) {
long totalSeconds = durationMs / 1000;
long days = totalSeconds / 86400;
// 如果天数大于0则构建返回字符串
if (days > 0) {
return days + "";
}
// 如果天数不足1天根据需求可以返回空字符串也可以返回 "0天" 或其他
return "1天"; // 或者 return "0天";
}
/**
* 计算时间差值并向上取整为天数
* @param currentTimeMillis 当前时间(毫秒级时间戳)
* @param createTimeSecond 创建时间(秒级时间戳)
* @return 向上取整后的天数
*/
public static int calculateDays(long currentTimeMillis, long createTimeSecond) {
// 1. 计算时间差(毫秒)
long timeDiffMillis = currentTimeMillis - createTimeSecond;
// 2. 转换为天数1天 = 24*60*60*1000 毫秒)
BigDecimal daysDecimal = new BigDecimal(timeDiffMillis)
.divide(new BigDecimal(24 * 60 * 60 * 1000), 10, RoundingMode.HALF_UP);
// 3. 向上取整(即使是 8.0001 天也会变成 9 天)
return daysDecimal.setScale(0, RoundingMode.CEILING).intValue();
}
/**
* 根据生日字符串计算年龄
* @param birthDay 生日字符串,格式为 "yyyy-MM-dd"
* @return 年龄
*/
public static int getAgeByBirthDay(String birthDay) {
if (birthDay == null || birthDay.isEmpty()) {
return 0;
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date birthDate = sdf.parse(birthDay);
Calendar cal = Calendar.getInstance();
int currentYear = cal.get(Calendar.YEAR);
int currentMonth = cal.get(Calendar.MONTH);
int currentDay = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDate);
int birthYear = cal.get(Calendar.YEAR);
int birthMonth = cal.get(Calendar.MONTH);
int birthDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
int age = currentYear - birthYear;
// 如果当前月份小于生日月份或者月份相同但当前日期小于生日日期则年龄减1
if (currentMonth < birthMonth ||
(currentMonth == birthMonth && currentDay < birthDayOfMonth)) {
age--;
}
return age < 0 ? 0 : age;
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
}