栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

18位时间戳改为Java时间,以及13位时间戳转换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

18位时间戳改为Java时间,以及13位时间戳转换


    cn.hutool
    hutool-all
    5.7.16

引入架包

package com.timestamp.test;
 
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;
 
@Slf4j
public class Test {
 
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
 
    private static final long TICKS_AT_EPOCH_NT = 116444736000000000L;
 
    private static final long TICKS_PER_MILLISECOND = 10000;
 
    private static TimeZone TIME_ZONE = TimeZone.getDefault();
 
    private static final long TICKS_AT_EPOCH = 621355968000000000L;
 
    
    public static String fromNTTimeToJdate(String str) {
        String zero = "0";
        if (StrUtil.isEmpty(str)) {
            return null;
        } else if (zero.equals(str)) {
            return zero;
        } else {
            Calendar calendar = Calendar.getInstance(TIME_ZONE);
            calendar.setTimeInMillis((Long.parseLong(str) - TICKS_AT_EPOCH_NT) / TICKS_PER_MILLISECOND);
            calendar.setTimeInMillis(calendar.getTimeInMillis() - calendar.getTimeZone().getRawOffset());
            return (new SimpleDateFormat(DATE_FORMAT)).format(calendar.getTime());
        }
    }
 
    
    public static String fromDnetToJdate(String str) {
        String zero = "0";
        if (StrUtil.isEmpty(str)) {
            return null;
        } else if (zero.equals(str)) {
            return zero;
        } else {
            Calendar calendar = Calendar.getInstance(TIME_ZONE);
            calendar.setTimeInMillis((Long.parseLong(str)-TICKS_AT_EPOCH)/TICKS_PER_MILLISECOND);
            calendar.setTimeInMillis(calendar.getTimeInMillis()-calendar.getTimeZone().getRawOffset());
            return (new SimpleDateFormat(DATE_FORMAT)).format(calendar.getTime());
        }
    }
 
    public static void main(String[] args) {
        String ntStr = "131974608035554296";
        log.info("NT时间戳:" + fromNTTimeToJdate(ntStr));
        String dntStr = "635210495600000000";
        log.info(".net18位时间戳转换:" + fromDnetToJdate(dntStr));
    }
 
}

13位时间戳

String s="1565499360620";
        Long timeStamp = Long.valueOf(s);  //获取当前时间戳
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));      // 时间戳转换成时间
        System.out.println("格式化结果:" + sd);

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒");
        String sd2 = sdf2.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
        System.out.println("格式化结果:" + sd2);

原文链接:https://blog.csdn.net/qq_37552993/article/details/88683056

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/601973.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号