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

Java 时间戳和时间的转换

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

Java 时间戳和时间的转换

工具类DateAndStampUtils:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateAndStampUtils {
    
    public static String dateToStamp(String time) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }


    
    public static String stampToDate(String stamp) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(stamp);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    
    public static String addTime(String time, int minute) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, minute);//增加minute分钟
        System.out.println(format.format(c.getTime()));
        return format.format(c.getTime());
    }


    
    public static Long timeGap(String oldTime, String NewTime) {
        Long time1 = Long.valueOf(oldTime);
        Long time2 = Long.valueOf(NewTime);
        return time2 - time1;
    }

    
    public static boolean calculateStamp(String oldTime, String NewTime, int minute) {
        long gap = timeGap(oldTime, NewTime);
        long allStamp = minute * 60000;
        if (gap <= allStamp) {
            return  false;
        }
        return true;
    }
}

测试:

public class MainServer {
    public static void main(String[] args) {
        //获取当前时间戳
        Long nowStamp = System.currentTimeMillis();
        System.out.println("当前时间戳是:"+nowStamp);

        //时间转时间戳
        String timeStr = "2021-09-29 14:46:37";
        String stamp = DateAndStampUtils.dateToStamp(timeStr);
        System.out.println("2021-09-29 14:46:37转为时间戳是:"+stamp);

        //时间戳转换为时间
        String stampStr = "1632897997000";
        String time = DateAndStampUtils.stampToDate(stampStr);
        System.out.println("1632897997000转为时间是:"+time);

        //计算两个时间戳的差是否超过1分钟
        //1632897877000代表时间2021-09-29 14:44:37,1632897997000代表时间2021-09-29 14:46:37
        boolean whetherExceed = DateAndStampUtils.calculateStamp("1632897877000", "1632897997000", 1);
        if (whetherExceed) {
            System.out.println("从1632897877000到1632897997000已经超过了1分钟");
        }else {
            System.out.println("从1632897877000到1632897997000没有超过1分钟");
        }
    }
}

输出结果:

当前时间戳是:1632970115657
2021-09-29 14:46:37转为时间戳是:1632897997000
1632897997000转为时间是:2021-09-29 14:46:37
从1632897877000到1632897997000已经超过了1分钟
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/281926.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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