JAVA 时间戳

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

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

3.掌握java.util.Calendar日历类。

实例化

add方法

set方法

get方法

格式化输出

Calendar c = Calendar.getInstance();

System.out.printf("%tF %<tT %<tB %<tA %<tp%n", c);

建立指定的日历

var c = Calendar.getInstance();

//1995 10 20 18:30:50

c.set(1995, Calendar.OCTOBER, 20, 18, 30, 50);

System.out.printf("%tF %<tT", c);

输出7天前日期

输出5天前日期

var c = Calendar.getInstance();

//两年前

c.add(Calendar.YEAR, -2);

//10天后

c.add(Calendar.DATE, 10);

System.out.printf("%tF %<tT %<tA %<tB %<tp", c);

转时间戳

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 %<tT:%s%n", ti, msg);

}

}

}

相关推荐
h***04771 小时前
SpringBoot(7)-Swagger
java·spring boot·后端
v***91302 小时前
Spring boot创建时常用的依赖
java·spring boot·后端
xlq223225 小时前
22.多态(上)
开发语言·c++·算法
666HZ6665 小时前
C语言——高精度加法
c语言·开发语言·算法
代码or搬砖5 小时前
MyBatisPlus讲解(二)
java·mybatis
星释5 小时前
Rust 练习册 100:音乐音阶生成器
开发语言·后端·rust
lcu1115 小时前
Java 学习42:抽象
java
Mr.朱鹏5 小时前
RocketMQ安装与部署指南
java·数据库·spring·oracle·maven·rocketmq·seata
雨中飘荡的记忆5 小时前
Spring表达式详解:SpEL从入门到实战
java·spring
Coder-coco5 小时前
个人健康管理|基于springboot+vue+个人健康管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·mysql·论文