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);

}

}

}

相关推荐
mengge.cloud1 小时前
Python基础保姆级!从入门到入土
开发语言·python·云原生·pygame
Resean02232 小时前
Spring 源码分析(六)循环依赖与三级缓存
java·spring·缓存
在爪洼山玩泥巴2 小时前
fastjson2 JSONPath 取不存在路径,为什么会抛 “not support”?
java·开发语言·jsonpath·fastjson
星落梦生12 小时前
SpringAOP基础笔记(7)
java·开发语言
金銀銅鐵13 小时前
带您看懂字节码指令(四):一条指令里有什么
java·jvm
最强小杰13 小时前
某 OpenAI 兼容网关上的 `kimi-k3.1-pro`:Cline、Zed 接入与 SSE 排错示例
java·服务器·网络·ai
Flynn Liang14 小时前
流量治理不是限流那么简单:I/O 密集型 Java 服务的 6 层防线
java·微服务·高并发·限流·熔断·系统稳定性·resilience4j
学长毕设14 小时前
基于SpringBoot智慧养老服务平台设计与实现-附源码
java·spring boot·后端
曹牧14 小时前
Java:“刷新雪崩”
java·开发语言
郝学胜-神的一滴14 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl