java中如何获取时间戳的方法实例
作者:Dr_Joseph 时间:2021-06-14 15:29:16
前言
数字时间戳技术是数字签名技术一种变种的应用。是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数(引用自百度百科)
在java开发过程中经常会遇到统计某一天或是某一个月的数据,因此常常需要获取截取数据的两个时间戳(比如统计今天的数据,则需要获取一个开始时间为今天零点以及一个结束时间为明天零点),然后根据数据相关的时间是否在该时间区间内来判断是否将其计入统计数据中。
一、java获取时间戳
话不多说,首先我们先拿上面的例子说起吧。如何获取今天零点以及明天零点的两个时间戳。
public Long getToday(){
DateTime now = new DateTime();
return new DateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0, 0).getMillis();
}
public Long getTomorrow(){
DateTime now = new DateTime();
return new DateTime(now.getYear(), now.getMonthOfYear(), now.getDayOfMonth(), 0, 0, 0, 0).plusDays(1).getMillis();
}
上面的方法中用到了DateTime中的plusDays(),同理,你如果需要获取下 个星期(年,月,时,分,秒,毫秒)前的时间戳,都有同样的plusYears(int X),plusMonths(int X)等等与之对应,如果要获取今天之前的就把传入一个负整数参数即可。
然而很多时候我们需要某个特定时间的时间戳,比如这个月5号14点23分6秒138毫秒的时间戳(这个时间并没有特殊的含义,随便选的)。
public Long getTime(){
Long now = new Date().getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
calendar.set(Calendar.DAY_OF_MONTH, 5);
calendar.set(Calendar.HOUR, 14);
calendar.set(Calendar.MINUTE, 23);
calendar.set(Calendar.SECOND, 6);
calendar.set(Calendar.MILLISECOND, 138);
return calendar.getTimeInMillis();
}
再比如我们可能需要知道这个星期二的10点10分10秒的时间戳。
public Long getTime(){
Long now = new Date().getTime();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(now);
calendar.set(Calendar.DAY_OF_WEEK, 2);
calendar.set(Calendar.HOUR, 10);
calendar.set(Calendar.MINUTE, 10);
calendar.set(Calendar.SECOND, 10);
return calendar.getTimeInMillis();
}
二、Java中两种获取精确到秒的时间戳的方法
Java中的时间戳的毫秒主要通过最后的三位来进行计量的,下面给大家分享从网上整理的两种不同的方式将最后三位去掉。
方法一:通过String.substring()方法将最后的三位去掉
/**
* 获取精确到秒的时间戳
* @return
*/
public static int getSecondTimestamp(Date date){
if (null == date) {
return 0;
}
String timestamp = String.valueOf(date.getTime());
int length = timestamp.length();
if (length > 3) {
return Integer.valueOf(timestamp.substring(0,length-3));
} else {
return 0;
}
}
方法二:通过整除将最后的三位去掉
/**
* 获取精确到秒的时间戳
* @param date
* @return
*/
public static int getSecondTimestampTwo(Date date){
if (null == date) {
return 0;
}
String timestamp = String.valueOf(date.getTime()/1000);
return Integer.valueOf(timestamp);
}
来源:https://segmentfault.com/a/1190000011795317
标签:java,时间戳,获取
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA基础之一些不为人知的那些秘密
2023-11-29 07:49:29
![](https://img.aspxhome.com/file/2023/8/59748_0s.jpg)
浅谈java中String StringBuffer StringBuilder的区别
2023-11-29 13:34:40
Java环境下高德地图Api的使用方式
2022-06-13 06:43:59
![](https://img.aspxhome.com/file/2023/6/85826_0s.jpg)
Spring框架通过工厂创建Bean的三种方式实现
2022-11-23 11:29:54
![](https://img.aspxhome.com/file/2023/7/70527_0s.png)
spring boot多数据源动态切换代码实例
2022-03-11 00:37:09
![](https://img.aspxhome.com/file/2023/7/71867_0s.png)
解析Android中webview和js之间的交互
2023-07-27 17:16:50
java中抽象类和接口的相同和不同点介绍
2021-05-30 04:53:31
RabbitMQ延迟队列及消息延迟推送实现详解
2023-11-15 15:31:31
![](https://img.aspxhome.com/file/2023/1/59001_0s.png)
Android实战教程第九篇之短信高效备份
2022-02-14 12:59:20
Java函数式编程(六):Optional
2021-07-04 21:44:05
IDEA类与方法注释模板设置图文教程(非常详细)
2022-02-24 10:18:29
![](https://img.aspxhome.com/file/2023/9/94189_0s.png)
Java中一个for语句导致无穷大死循环的例子
2022-12-17 08:13:33
Spring Cloud Config实现分布式配置中心
2022-04-25 16:28:07
mybatis源码解读-Java中executor包的语句处理功能
2023-09-03 06:34:16
asp.net实现遍历Request的信息操作示例
2022-11-15 23:15:18
![](https://img.aspxhome.com/file/2023/3/126053_0s.png)
c#批量上传图片到服务器示例分享
2022-09-12 10:16:24
Java多线程 volatile关键字详解
2023-07-16 02:13:31
![](https://img.aspxhome.com/file/2023/4/125644_0s.png)
使用Spring Boot进行单元测试详情
2023-11-10 08:01:53
浅析java移位符的具体使用
2023-12-21 09:36:13
jsp如何获取Session中的值
2023-07-01 05:59:28