时间util

该博客主要介绍Java中时间处理相关内容,包括获取当前毫秒数、毫秒数与时间的相互转换、时间格式化、从字符串格式时间获取毫秒数、毫秒数计算,还提及了时间插入数据库的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:https://blog.youkuaiyun.com/sinat_32238399/article/details/80512452

后期将持续更新 

1.获取当前毫秒数

long t1=System.currentTimeMillis();

2、毫秒数转换为时间

Date date2=new Date();
date2.setTime(t1);
System.err.println(date2);

3.时间格式化

​
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String fmDate=simpleDateFormat.format(date2);
System.err.println(fmDate);

​

4.字符串格式时间获取毫秒数

String sdate = "2018-06-01 06-06-06";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
long time = simpleDateFormat.parse(sdate).getTime();
System.err.println(time);

5.毫秒数计算

/**
	 * 将毫秒数转为时分秒的形式
	 * @param 要转换的毫秒数
	 * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
	 */
	public static String formatDuring(long mss) {
		long days = mss / (1000 * 60 * 60 * 24);
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return days + " days " + hours + " hours " + minutes + " minutes "
				+ seconds + " seconds ";
	}
	/**
	 * 
	 * @param begin 时间段的开始
	 * @param end	时间段的结束
	 * @return	输入的两个Date类型数据之间的时间间格用* days * hours * minutes * seconds的格式展示
	 */
	public static String formatDuring(Date begin, Date end) {
		return formatDuring(end.getTime() - begin.getTime());
	}

出处:https://blog.youkuaiyun.com/sunshinestation/article/details/4568946

 6.时间插入数据库

Date date=new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String fmDate=simpleDateFormat.format(date);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值