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

}

}

}

相关推荐
万粉变现经纪人几秒前
如何解决pip安装报错ModuleNotFoundError: No module named ‘python-dateutil’问题
开发语言·ide·python·pycharm·pandas·pip·httpx
IT乐手6 分钟前
Java 实现异步转同步的方法
java
杨杨杨大侠6 分钟前
附录 1:🚀 Maven Central 发布完整指南:从零到成功部署
java·github·maven
Sammyyyyy11 分钟前
macOS是开发的终极进化版吗?
开发语言·macos·开发工具
青草地溪水旁22 分钟前
23 种设计模式
开发语言·c++·设计模式
草履虫建模25 分钟前
在 RuoYi 中接入 3D「园区驾驶舱」:Vue2 + Three.js + Nginx
运维·开发语言·javascript·spring boot·nginx·spring cloud·微服务
编码浪子25 分钟前
趣味学RUST基础篇(函数式编程闭包)
开发语言·算法·rust
渣哥28 分钟前
Java HashMap 扩容机制详解:触发条件与实现原理
java
赵星星52028 分钟前
Spring Bean线程安全陷阱:90%程序员都会踩的坑,你中招了吗?
java
MC皮蛋侠客35 分钟前
使用python test测试http接口
开发语言·python·http