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

}

}

}

相关推荐
aqiu11111135 分钟前
【C++算法打怪专栏】8. 字符串转换整数 (atoi)
开发语言·c++·算法
爱吃香菜的初学者42 分钟前
十四.Linux——消息队列
linux·运维·开发语言
迅猛龙办公室1 小时前
python实现倒序输出数字
开发语言·python
夏幻灵7 小时前
JavaScript this 面试:五种绑定规则、优先级与常见面试陷阱
开发语言·javascript·面试
程序猿编码7 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
bkspiderx8 小时前
Qt 插件机制:动态扩展应用功能的核心框架
开发语言·qt·元数据·qt 插件·qpluginloader
代码什么用9 小时前
Spring基础使用
java·后端·spring
程序员老陆9 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
沐晓时光9 小时前
C语言入门,深入理解指针(3)
c语言·开发语言
Su米苏9 小时前
Spring AI 中MCP 与普通 @Tool的区别
java·人工智能·spring