恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

TimeUtils记录

  • 首页
  • 资讯中心
  • /
  • TimeUtils记录

相关资讯

ownCloud高危漏洞CVE-2023-49103修复与安全加固实战指南 2026/8/8 16:41:47
NVM 管理Node.js版本 2026/8/8 16:41:47
简单图判断 2026/8/8 16:41:47

最新资讯

HAL模型参数详解:4096个六聚体特征如何决定剪接位点强度
acme.sh Shell脚本方案:自动化SSL证书管理的终极指南
容度原理解读:月球矿藏——被“潮汐锁定”封印的46亿年容度记录
如何用Malinois快速预测CRE活性?3分钟上手教程
3个颠覆性技巧:用QtScrcpy将手机游戏变成PC级体验
Trae Beta版接入 Spring Boot 后端:MCP 协议配置与代码生成质量治理

今日推荐

Java图像处理实战指南
昇腾AI代理实现多号通话自动化
2026年Graph+AI Agents最新创新思路

本周热门

ncmdumpGUI:一键解锁网易云音乐ncm文件的终极解决方案
分布式配置中心选型实战:Nacos与Consul在创业场景下的对比
MoneyPrinterPlus实战指南:AI视频批量生成与自动化发布完整解决方案

本月精选

如何用DamaiHelper实现演唱会门票的智能自动化抢购:完整技术解决方案指南
第4篇:59 倍性能差距的索引瓶颈定位——一次教科书级的全表扫描调优
终极歌词批量下载神器:5分钟解决离线音乐库歌词同步难题

TimeUtils记录

发布时间:2026/8/8 16:41:47
TimeUtils记录 /* long milliseconds 1500L; Log.d(Time, 四舍五入: TimeUtils.millisToSecondsRounded(milliseconds)); // 输出 2 Log.d(Time, 截断取整: TimeUtils.millisToSecondsTruncated(milliseconds)); // 输出 1 Log.d(Time, 向上取整: TimeUtils.millisToSecondsCeil(milliseconds)); // 输出 2 */ public class TimeUtils { // 四舍五入 public static long millisToSecondsRounded(long millis) { return Math.round(millis / 1000.0); } // 截断取整 public static long millisToSecondsTruncated(long millis) { return millis / 1000; } // 向上取整 public static long millisToSecondsCeil(long millis) { return (long) Math.ceil(millis / 1000.0); } /** * 将字符串灵活转换为毫秒时间戳 * param input 可能是日期时间字符串如 2026-07-23 06:00:08或数字字符串如 1758492008000 * return 毫秒时间戳 * throws IllegalArgumentException 如果无法解析 */ public static long toMilliseconds(String input) { if (StringUtils.isEmpty(input)) { return 0; } // 1. 尝试当作数字时间戳处理 if (isNumeric(input)) { try { return ensureMillisTimestamp(Long.parseLong(input)); // 改为调用新方法 } catch (NumberFormatException e) { throw new IllegalArgumentException(无法解析为时间戳数字: input, e); } } // 2. 否则按日期时间格式解析 DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss); try { LocalDateTime ldt LocalDateTime.parse(input, formatter); // 使用系统默认时区转时间戳 return ldt.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); } catch (DateTimeParseException e) { throw new IllegalArgumentException(无法解析日期时间字符串: input, e); } } public static String toMillisecondsStr(String input) { return toMilliseconds(input) ; } /** * 确保时间戳是毫秒级如果不是如秒级10位则自动转为毫秒级 * p * 判断逻辑时间戳值小于 1000000000000L10^12视为秒级乘以1000转为毫秒 * 大于等于该阈值的视为已是毫秒级直接返回。 * * param timestamp 时间戳可能是秒级10位或毫秒级13位 * return 毫秒级时间戳 */ public static long ensureMillisTimestamp(long timestamp) { if (timestamp 0) { return timestamp; } // 小于 10^12 认为是秒级时间戳乘以 1000 转为毫秒 if (timestamp 1000000000000L) { return timestamp * 1000; } return timestamp; } /** * 确保时间戳是毫秒级String版本 * p * 先将字符串解析为 long再调用 {link #ensureMillisTimestamp(long)} 进行转换。 * * param timestampStr 时间戳字符串可能是秒级10位或毫秒级13位 * return 毫秒级时间戳解析失败返回 0 */ public static long ensureMillisTimestamp(String timestampStr) { if (StringUtils.isEmpty(timestampStr)) { return 0; } try { long timestamp Long.parseLong(timestampStr.trim()); return ensureMillisTimestamp(timestamp); } catch (NumberFormatException e) { return 0; } } /** * 支持指定时区的版本 * param input 同上 * param zoneId 时区ID如 Asia/Shanghai、UTC * return 毫秒时间戳 */ public static long toMilliseconds(String input, String zoneId) { if (isNumeric(input)) { return Long.parseLong(input); } DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss); LocalDateTime ldt LocalDateTime.parse(input, formatter); return ldt.atZone(ZoneId.of(zoneId)).toInstant().toEpochMilli(); } // 辅助方法判断字符串是否表示整数允许负号 private static boolean isNumeric(String str) { if (str null || str.isEmpty()) return false; int start (str.charAt(0) -) ? 1 : 0; for (int i start; i str.length(); i) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } /** * 计算年龄最正确的方式是用 LocalDate Period而不是时间戳相减 * param birthday 2025-7-5 * return */ public static int calculateAge(String birthday) { try { DateTimeFormatter formatter DateTimeFormatter.ofPattern(yyyy-MM-dd); LocalDate birthDate LocalDate.parse(birthday, formatter); // 以“今天”为基准计算年龄 LocalDate today LocalDate.now(); return Period.between(birthDate, today).getYears(); } catch (Exception e) { return -1; } } }毫秒转秒取整在 Java 或 Android 中将毫秒转换为秒并取整四舍五入或直接截断可以通过以下方式实现1. 四舍五入取整long milliseconds 12345L; // 示例毫秒值 long seconds Math.round(milliseconds / 1000.0); // 四舍五入说明Math.round() 对浮点数结果进行四舍五入。除以 1000.0 确保结果是浮点数避免整数除法截断。2. 直接截断取整向下取整long milliseconds 12345L; long seconds milliseconds / 1000; // 直接截断说明整数除法会自动丢弃小数部分相当于 Math.floor()。3. 向上取整long milliseconds 12345L; long seconds (long) Math.ceil(milliseconds / 1000.0); // 向上取整注意事项精度问题如果毫秒值是 int 类型注意避免整数溢出例如使用 long 存储大数值。负数处理上述方法同样适用于负数例如 -1500ms 会转换为 -1s 或 -2s取决于取整方式

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号