java获取时间戳单位秒,如何测试给定的时间戳是以秒或毫秒为单位?「建议收藏」

java获取时间戳单位秒,如何测试给定的时间戳是以秒或毫秒为单位?「建议收藏」Assumeagivenvariable,itiscontainingaUNIXtime-stamp,butwhetheritisinsecondsormillisecondsformatisunknown,IwanttoassigntoavariablewhichisinsecondsformatForExample:unkno…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

java获取时间戳单位秒,如何测试给定的时间戳是以秒或毫秒为单位?「建议收藏」

Assume a given variable, it is containing a UNIX time-stamp, but whether it is in seconds or milliseconds format is unknown, I want to assign to a variable which is in seconds format

For Example:

unknown = 1398494489444 # This is millisecond

t = ???

Updated: I understand it is not possible to tell without giving some limitations, so here is it

current_ts – 86400 * 365 < unknown < current_ts

Assume current_ts = current unix timestamp

解决方案

If you convert the maximum timestamp values with x digits in millis you get something like this:

9999999999999 (13 digits) means Sat Nov 20 2286 17:46:39 UTC

999999999999 (12 digits) means Sun Sep 09 2001 01:46:39 UTC

99999999999 (11 digits) means Sat Mar 03 1973 09:46:39 UTC

Can your timestamps be older than 2001? If not i think you’re safe to check whether the number has 13 digits or more – if yes you have milliseconds, if not you have seconds. Of course this will only work until timestamps in seconds will have 13 digits as well, which means timestamps in milliseconds will have 16 digits:

1000000000000000 (16 digits) means Fri Sep 27 33658 01:46:40 but by then i will be living on a planet from the Alpha Centauri system and time standards will probably have changed a bit :)

P.S. you can loosen up the condition to 12 digits or more, if your timestamps can’t go back further than 1973. The condition should only be ok up until:

100000000000000 (15 digits) means Wed Nov 16 5138 09:46:40

because this in seconds will have 12 digits and will overlap with your condition

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197348.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 三种查看MySQL数据库版本的方法

    三种查看MySQL数据库版本的方法

    2021年10月18日
    86
  • VScode快捷键和设置

    VScode快捷键和设置一.快捷键单行注释 ctrl+/ 多行注释 alt+shift+a 复制上一行代码到下一行 alt+shift+↓ 移动代码到上一行或下一行 alt+↑,alt+↓ 返回到上次编辑位置 alt+← 跳转到指定文件 ctrl+p 全文搜索字符 ctrl+shift+f 格式化代码 shift+alt+f…

    2022年4月30日
    65
  • Python3列表_python tuple

    Python3列表_python tuple列表列表特点:是一种序列结构,与元组不同,列表具有可变性,可以追加、插入、删除、替换列表中的元素新增元素appendappend添加一个对象,可以是任意类型a=['zhangsa

    2022年7月31日
    6
  • 好中层的八个标准:如何成为一名优秀的管理者

    好中层的八个标准:如何成为一名优秀的管理者 4月底公司给所有CSDN中层干部做了两天《如何成为一名优秀的管理者》的系统培训,讲师是胡斌老师,胡斌老师有多年的IT行业从业经验,讲的还是非常不错的,不过收尾有点仓促,比较可惜。讲完之后,蒋涛又接着给我们送了一本胡老师推荐的数《格鲁夫:给经理人的第一堂课》。利用五一的时间,结合培训和这本书,对以前的管理工作进行了反思,发现真正的要做一个好的中层真的是非常不容易,总结了一下,可以用八个字来表达“上

    2022年6月3日
    57
  • jedis链接redis_redis和jedis关系

    jedis链接redis_redis和jedis关系1.导入jedis包<dependencies> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency> <!–fastjson–> <depend

    2025年10月11日
    2
  • 如何彻底卸载Anaconda?[通俗易懂]

    如何彻底卸载Anaconda?[通俗易懂]文章目录0.前言1.解决方案1.1方案11.2方案22.参考文档0.前言  本机Win10。  最好的参考文档是官方文档UninstallingAnaconda。除此以外,再辅助以其他文档,就能达到如标题所示的目的。整个删除过程不难。1.解决方案1.1方案1  官方推荐的方案是两种,一种是直接在Anaconda的安装路径下,双击或者说是直接到控制面板找到Anaconda直接卸载就行了。但是这种方案并不能删除配置文件。1.2方案2  但是,如果你搜索到了这篇文章,就代表你是想把配置文件都

    2022年6月24日
    200

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号