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

}

}

}

相关推荐
秋91 分钟前
Go语言(Golang)开发工程师全景解析:岗位职责·语言优势与使用场景·各城市薪资·发展前景·高考志愿填报(2026版)
开发语言·golang·高考
huangdong_1 小时前
1688商品图片采集技术解析:登录态处理与SKU图自动分类
开发语言
马士兵教育1 小时前
Java还有前景吗?Java+AI大模型学习路线及项目?
java·人工智能·python·学习·机器学习
chase_my_dream1 小时前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
snow@li1 小时前
Java:理解 Gradle / 后端项目的管家 / 打包SpringBoot 应用 / 完成编译、下载依赖、运行测试、打包 JAR/WAR / 速查表
java
Cloud_Shy6181 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
云烟成雨TD2 小时前
Spring AI 1.x 系列【57】动态工具发现:Tool Search Tool
java·人工智能·spring
zfoo-framework2 小时前
[修改代码使用]codex官方app中使用中转(不需要cc-switch) 1.config.toml 2.sk方式登录
java
天佑木枫2 小时前
15天Python入门系列 · 序
开发语言·python
逍遥德2 小时前
MQTT教程详解-05.SpringBoot集成mqtt client 性能分析
java·spring boot·spring·mt