【Java】时间戳与Date相互转换
时间戳转Date
public static void main(String[] args) {
// 10位的秒级别的时间戳
long time1 = 1527767665;
String result1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time1 * 1000));
System.out.println("10位数的时间戳(秒)--->Date:" + result1);
Date date1 = new Date(time1*1000); //对应的就是时间戳对应的Date
// 13位的秒级别的时间戳
double time2 = 1515730332000d;
String result2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time2);
System.out.println("13位数的时间戳(毫秒)--->Date:" + result2);
}
10位数的时间戳(秒)--->Date:2018-05-31 19:54:25
13位数的时间戳(毫秒)--->Date:2018-01-12 12:12:12
尤其要注意上面10位的秒级别的时间戳时,不能用int来定义time1变量,否则会得到错误的结果:
public static void main(String[] args) {
// 10位的秒级别的时间戳
int time1 = 1527767665; //错误做法
String result1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(time1 * 1000));
System.out.println("10位数的时间戳(秒)--->Date:" + result1);
}
10位数的时间戳(秒)--->Date:1969-12-17 23:21:47
Date转时间戳
public static void main(String[] args) {
//获取指定时间的时间戳,除以1000说明得到的是秒级别的时间戳(10位)
long time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse("2018-06-30 20:00:00", new ParsePosition(0)).getTime() / 1000;
//获取时间戳
long now1 = System.currentTimeMillis();
long now2 = new Date().getTime();
System.out.println("获取指定时间的时间戳:" + time);
System.out.println("当前时间戳:" +now1);
System.out.println("当前时间戳:" +now2);
}
获取指定时间的时间戳:1530360000
当前时间戳:1527769494340
当前时间戳:1527769494340
格式化Date
public static void main(String[] args) {
//使用common-lang包下面的DateFormatUtils类
String format1 = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
//使用最原始的SimpleDateFormat类
String format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
System.out.println("格式化时间1:" + format1);
System.out.println("格式化时间2:" + format2);
}
格式化时间1:2018-05-31 20:26:49
格式化时间2:2018-05-31 20:26:49
DateFormatUtils是commons.lang3.time.DateFormatUtils下的,如果你的项目中没有,maven中引入下:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
给日期加上指定时长
比如,给现在的时间加上12个小时,这个需求也很常见,例如我做过的某个秒杀接口要返回剩余秒杀时间给前端,那么我就直接计算(比如秒杀持续时间为12小时):秒杀(倒计时)截止时间=(当前时间+12H)即可。
public static void main(String[] args) {
//将指定日期加上固定时间,DateUtils还有其它添加分钟、小时、月份之类的方法api
//使用到的是commons-lang包下面的DateUitls类
Date date = DateUtils.addDays(new Date(), 10); //
System.out.println("当前时间为:"+DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
String format = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
System.out.println("当前时间加上10天后:" + format);
}
当前时间为:2018-05-31 20:31:53
当前时间加上10天后:2018-06-10 20:31:53
得到指定时间节点的日期时间
方式一
public static void main(String[] args) throws ParseException {
//得到指定日期
String date = "2018-03-03 15:20:12";
Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
System.out.println(parse);
}
方式二
/**
* 获取当天的YYYY-MM-dd 00:00:01 时间点的毫秒级时间戳
*
* @return
*/
public static Long getCurrentBeginDate() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);//控制时
cal.set(Calendar.MINUTE, 0);//控制分
cal.set(Calendar.SECOND, 1);//控制秒
return cal.getTimeInMillis();
}
判断两个时间点是否为同一天、同一年
给定两个日期,快速判断两者是否为同一天或者同一年,比如我做过的一个接口需求是:每人每天有一次抽奖机会,那么当用户当天第二次发送请求时候,我就得判断查询参与记录,并且判断最新一次参与时间和当天是否为同一天。
/**
* 判断是否为同一天:使用commons-lang包下的DateUtils类
*
* @param day1
* @param day2
* @return
*/
public boolean isSameDay(Date day1, Date day2) {
return DateUtils.isSameDay(day1, day2);
} /**
* 判断是否为同一天:使用joda依赖包里的时间类,效率从一定程度上优于DateUtils.isSameDay()方法
*
* @param date1
* @param date2
* @return
*/
public static boolean isSameDay1(Date date1,Date date2){
if(date1==null || date2==null){
throw new IllegalArgumentException("date must be not null");
}
LocalDate localDate1 = new LocalDate(new DateTime(date1.getTime()));
LocalDate localDate2 = new LocalDate(new DateTime(date2.getTime()));
return localDate1.equals(localDate2);
}
【Java】时间戳与Date相互转换的更多相关文章
- java 时间戳与date转换
1.时间戳转换为date long sjc=1442633777; SimpleDateFormat t = new SimpleDateFormat("yyyyMMddHHmmss&quo ...
- java时间戳转date(转)
1.时间戳的定义 时间戳(timestamp),通常是一个数字序列,唯一地标识某一刻的时间,指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起 ...
- java 时间戳与日期字符串相互转换
/** * 时间戳转换成日期格式字符串 * @param seconds 精确到秒的字符串 * @param formatStr * @return */ public static String t ...
- c# DateTime时间格式和JAVA时间戳格式相互转换
/// java时间戳格式时间戳转为C#格式时间 public static DateTime GetTime(long timeStamp) { DateTime dtStart = TimeZon ...
- java 时间戳和PHP时间戳 的转换
java 时间戳和PHP时间戳 的转换 PHPJava 总结一下java 时间戳和PHP时间戳 的转换问题: 由于精度不同,导致长度不一致,直接转换错误. JAVA时间戳长度是13位,如:12948 ...
- Java时间戳与日期格式字符串的互转
上代码: import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { /** * 时间戳转换成日 ...
- PHP时间戳与时间相互转换(精确到毫秒)
原文:PHP时间戳与时间相互转换(精确到毫秒) /** 获取当前时间戳,精确到毫秒 */ function microtime_float(){ list($usec, $sec) = explo ...
- Java~时间戳小知识
大叔对java时间戳使用的总结 Java里的Date对象有方法setTime,主要是将一个时间戳转成一个日期对象,而这个时间戳的标准是unix标准,即当前时间与1970/1/1相差的毫秒数,记得是毫秒 ...
- java 时间戳和PHP时间戳 的转换[10位和13位]
2013-08-02 14:06 9826人阅读 评论(2) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. 总结一下java 时间戳和PHP时间戳 的转换问题: 由于精度不同,导 ...
随机推荐
- Duplicate entry '4799' for key 'PRIMARY'
增加1条SQL记录报错: Operation failed: There was an error while applying the SQL script to the database. Exe ...
- 16/8/21_PHP-有关类函数,this,static,面向对象思想介绍
class_exists():判断某个类是否存在(定义过) interface_existe():判断接口是否存在 get_class():获取某个对象的"所属类名" get_pa ...
- Nginx 介绍配置
nginx的功能和优缺点 nginx是一种服务器软件,将程序放在nginx服务器上,将程序发布出去,nginx是一种高性能的Http和反向代理服务器,同时也是一个代理邮件服务器,也可以实现负载均衡. ...
- Jmeter中Bean shell脚本格式修改为utf-8
遇到的问题: 在做 一个发贴的接口测试时发现,发送数字+纯字母贴子时,可以正常请求成功.但当贴内容为中文时,服务端编码为乱码??. 原因: jmeter中,shell脚本的默认的格式为GBK,所以我在 ...
- 如何配置JedisPool的参数
转自:http://blog.csdn.net/huahuagongzi99999/article/details/13631579 如何配置Pool的参数 JedisPool的配置参数很大程度上依赖 ...
- df认识
import pandas as pd #自己创建一个df df = pd.DataFrame({ ,,], 'col2':["zs",'li','zl'], 'col3':[3. ...
- 用Emacs编写mybatis
用Emacs编写mybatis */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} ...
- 验证客户端的合法性、socketserver模块
一.为了防止客户端被人非法利用,需要在使用之前对客户端进行合法性验证.接下来就是客户端验证的几种方法 hmac 加密方法 import socket import os import hmac #能 ...
- vue2.0在IE11无法打开的解决办法
npm 安装bebel-polyfill npm install --save-dev babel-polyfill 在webpack.base.conf.js文件中将 module.exports ...
- 屏幕坐标点转UGUI坐标【包含屏幕适配】
using UnityEngine; public class ScreenToUI : MonoBehaviour { public const float UI_Width = 1366f; pu ...