• JAVA 时间戳


    1.了解java.util.Date类使用

    2.了解java.text.SimpleDateFormat格式化日期类使用

    3.掌握java.util.Calendar日历类。
       实例化
       add方法
       set方法
       get方法
       格式化输出
            Calendar c = Calendar.getInstance();
            System.out.printf("%tF %

       建立指定的日历
        var c = Calendar.getInstance();
            //1995 10 20 18:30:50
            c.set(1995, Calendar.OCTOBER, 20, 18, 30, 50);
            System.out.printf("%tF %    输出7天前日期
       输出5天前日期
            var c = Calendar.getInstance();
            //两年前
            c.add(Calendar.YEAR, -2);
            //10天后
            c.add(Calendar.DATE, 10);
            System.out.printf("%tF %    转时间戳
            var c = Calendar.getInstance();
            long t1 = c.getTimeInMillis();
       转java.util.Date
           var c = Calendar.getInstance();
           Date dd = c.getTime();

    4.了解idea整个项目,查找 替换功能。
       ctrl + shift + f 查找
       ctrl + shift + r 替换

    5.实现微信信息提示的日期信息计算功能。
       刚刚 
       分钟前
       小时前
       天前
       xxxx-xx-xx xx:xx:xx 

        package cn.webrx.calendarexample;
        import java.text.ParseException;
        import java.text.SimpleDateFormat;

        public class Ex4 {
            public static void main(String[] args) throws ParseException {
            String time = "2023-10-11 2:05:00";
            String msg = "你好,在吗?";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long ti = sdf.parse(time).getTime();
            long nn = System.currentTimeMillis();

            if ((nn - ti) / 1000 / 60 < 1) { //<1分钟
                System.out.printf("刚刚:%s%n", msg);
            }

            long t1 = (nn - ti) / 1000 / 60;
            if (t1 >= 1 && t1 < 60) { //>1分钟 < 60分钟
                System.out.printf("%d分钟前:%s%n", t1, msg);
            }

            long t2 = (nn - ti) / 1000 / 60 / 60;
            if (t2 >= 1 && t2 < 24) {//小时
                System.out.printf("%d小时前:%s%n", t2, msg);
            }

            long t3 = (nn - ti) / 1000 / 60 / 60 / 24;
            if (t3 >= 1 && t3 <= 7) {
                System.out.printf("%d天前:%s%n", t3, msg);
            } else if (t3 > 7) {
                System.out.printf("%tF %         }
            }
        }
     

  • 相关阅读:
    正式发布丨VS Code 1.70
    A+轮融资近2亿元,本土线控制动「TOP 1」按下“加速键”
    电容笔好还是触屏笔好?便宜又好用的电容笔推荐
    RK3568驱动指南|第七期-设备树-第60章 实例分析:GPIO
    【linux】性能优化
    【洛谷P1966】火柴排队【树状数组,离散化,思维】
    数据结构与算法基础-学习-31-交换排序之冒泡排序、快速排序
    vulnhub靶场之DIGITALWORLD.LOCAL: VENGEANCE
    WEB自动化测试,一定得掌握的8个核心知识点
    2022团体程序设计天梯赛L1
  • 原文地址:https://blog.csdn.net/webxscan/article/details/133783006