1、Date对象转换为时间戳

Date date = new Date();
long times = date.getTime();
System.out.println(times);

效果如下:

1508824283292

2、时间戳转换为Date日期对象

long times = System.currentTimeMillis();
Date date = new Date(times);
System.out.println(date);

效果如下:

Tue Oct 24 13:49:28 CST 2017

3、时间戳转换为指定日期格式

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long times = System.currentTimeMillis();
String str = format.format(times);
System.out.println(str);

效果如下:

2017-10-24 13:50:46

4、时间字符串<年月日时分秒毫秒 >转为 时间戳

转为

代码:

//大写HH:24小时制,小写hh:12小时制
//毫秒:SSS
//指定转化前的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//转化后为Date日期格式
Date date = sdf.parse(sb.toString());
//Date转为时间戳long
long shootTime = date.getTime();
System.out.println(shootTime);

实例:获取数据库的BigInt类型的时间戳,并转为日期格式

package com.test;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar; public class Test { public static void main(String[] args) { Connection conn;
Statement stmt;
ResultSet rs;
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test;";
String sql = "select * from [test].[dbo].[student]";
try {
conn = DriverManager.getConnection(url, "sa", "Rfid123456");
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){ long times = rs.getLong("date");
System.out.println(times); Date date = new Date(times);
System.out.println(date); } if (rs != null) {
rs.close();
rs = null;
} if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("数据库连接失败");
} } }

Date与时间戳的相互转换(Java)的更多相关文章

  1. java中XMLGregorianCalendar类型和Date类型之间的相互转换

    import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import ja ...

  2. java SimpleDateFormat日期与时间戳的相互转换

    自我总结,有什么不到位的地方,各位可以帮忙纠正补充一下,感激不尽! 目的:SimpleDateFormat类可以很随意的组合日期时间的格式,不止单纯的yyyy-MM-dd这种格式 废话不多说,上代码 ...

  3. Spring 整合 Flex (BlazeDS)无法从as对象 到 Java对象转换的异常:org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.util.Date' to required type 'java.sql.Timestamp' for property 'wfsj'; nested exception is java.lang.Ill

    异常信息如下: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value ...

  4. java.util.Date日期类通过java语句转换成Sql(这里测试用的是oracle)语句可直接插入(如:insert into)的日期类型

    public void add(Emp emp) throws Exception{ QueryRunner runner = new QueryRunner(JdbcUtil.getDataSour ...

  5. java.sql.Date赋值给了java.util.Date.转化成JSONArray时出错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

    net.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject.defa ...

  6. Date与String的相互转换

    构造函数 日期:new Date();//获取当前日期,精确到毫秒. 日期:new Date(long date);//即1970 年 1 月 1 日 00:00:00 GMT(Greenwich M ...

  7. Mysql时间戳转Java时间戳

    MySQL 时间戳和Java返回的时间戳是不一样的 例如: 当前时间是 2014-08-04 10:42:55.204000 使用mysql时间戳函数UNIX_TIMESTAMP 返回的结果为: 14 ...

  8. java Date 当天时间戳处理

    1. 代码 private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; private static Date ...

  9. java中时间与时间戳的相互转换

    package com.test.one; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...

随机推荐

  1. Windows上安装运行hadoop

    0.自己编译安装步骤在这里,有英文版本连接:<英文传送门>. 自己编译尝试后不成功,换为下面使用别人编译好的版本的方法.参考博客:<初学hadoop,windows下安装> 1 ...

  2. Unity Shader入门精要之 screen post-processing effect

    本篇记录了学习Unity Shader入门精要的屏幕后处理的一些知识点. OnRenderImage(RenderTexture src, RenderTexture dest) 以上函数是Unity ...

  3. 第 7 章 多主机管理 - 045 - 安装 Docker Machine

    安装 Docker Machine 先安装docker 官方安装docker-machine的文档地址:https://docs.docker.com/machine/install-machine/ ...

  4. 第 3 章 镜像 - 016 - Dockerfile 常用指令

    Dockerfile 常用指令 1.FROM 指定base镜像2.MAINTAINER 设置镜像的作者,可以为任意字符串3.COPY 从build context 复制到镜像 COPY 支持两种形式: ...

  5. 雷林鹏分享:XML to HTML

    XML to HTML 在 HTML 页面中显示 XML 数据 在下面的实例中,我们打开一个 XML 文件("cd_catalog.xml"),然后遍历每个 CD 元素,并显示HT ...

  6. Genome-wide gene-environment analyses of depression and reported lifetime traumatic experiences in UK Biobank

    Genome-wide gene-environment analyses of depression and reported lifetime traumatic experiences in U ...

  7. caffe在win10下的安装与配置

    1.Windows环境caffe安装配置(无GPU) 参考:http://www.cnblogs.com/cxyxbk/p/5902034.html 解压caffe-windows文件,将./wind ...

  8. MySQL补充

    1.mysql限制显示条目数:Limit, offset 图片网址:https://sqlbolt.com/lesson/filtering_sorting_query_results 实例: SEL ...

  9. python面向对象之 类的关系

    内容梗概: 1. 依赖关系 2. 关联关系, 组合关系, 聚合关系 3. 继承关系, self到底是什什么鬼? 4. 类中的特殊成员 1. 依赖关系def:在方法中给方法传递一个对象. 此时类与类之间 ...

  10. 『计算机视觉』经典RCNN_其二:Faster-RCNN

    项目源码 一.Faster-RCNN简介 『cs231n』Faster_RCNN 『计算机视觉』Faster-RCNN学习_其一:目标检测及RCNN谱系 一篇讲的非常明白的文章:一文读懂Faster ...