Java时间对象的运用
2021/7/20 14:06:45
本文主要是介绍Java时间对象的运用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
计算 从出生 到现在 经过多少天
package Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//TODO 计算 从出生 到现在 经过多少天
public class DateDemo {
public static void main(String[] args) throws ParseException {
// 这是保存出生日期date格式
Date chusheng = new Date();
// 这是现在日期date格式
Date nowtime = new Date();
// 这是规定格式化日期
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日HH时mm分ss秒”);
// 获取出生日期
String DateOfBirth = getDateOfBirth();
// 将出生的格式化日期转变为date格式
chusheng = sdf.parse(DateOfBirth);
// 获取当前日期的毫秒
long nowseconds = nowtime.getTime();
// 获取出生日期的毫秒
long birthseconds = chusheng.getTime();
// 获取现在到出生相差的毫秒
long dirta = nowseconds - birthseconds;
System.out.println(“你现在出生已经” + days(dirta) + “天啦!”);
} public static String getDateOfBirth(){ Scanner in = new Scanner(System.in); System.out.println("请输入你的出生日期(xxxx年xx月xx日xx时xx分xx秒格式):"); return in.next(); } public static double days(long days) { // 一天是86400000毫秒 return days / 86400000.0; }
}
这篇关于Java时间对象的运用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南