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. python Django 创建应用

    如图输入如下命令 python manage.py startapp apitest 添加应用到 autotest项目项目下 在settings.pyo 中加入“apitest”,如下图 创建视图 在 ...

  2. 批标准化 Batch Normalization

    2018-12-05 20:28:15 在机器学习领域有一个很重要的假设,即独立同分布假设,也就是说训练集和测试集是满足相同分布的,这是通过训练数据获得的模型能够在测试集获得好的效果的一个基本保障.而 ...

  3. docker 安装完mysql 后客户端无法访问

    1.在虚拟机的centos 中安装 docker 的mysql 镜像. docker run --name mysql01 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=12 ...

  4. linux: 安装jdk(java)

    作为Java开发人员,在Linux下安装一些开发工具是必备技能,本文以安装jdk为例,详细记录了每一步的操作命令,以供参考. 0.下载jdk8 登录网址:http://www.oracle.com/t ...

  5. English Voice of <<City of stars>>

    City of stars 星光之城啊 Are you shining just for me? 你是否只愿为我闪耀 City of stars 星光之城啊 There's so much that ...

  6. Spring Cloud之注册中心搭建

    一.注册中心服务端搭建 1)引入相关Maven坐标 <dependency> <groupId>org.springframework.cloud</groupId> ...

  7. 【洛谷p2837】晚餐队列安排

    (一定要先贴一下wz大佬对这道题的定位:) 另外说一句:我终于在拖了nnnnnnnnn天之后做完了这道题 算法标签:(其实也用不到辽上面都有)但我就是要贴一下咬我啊) 好啦好啦,上 思路: 首先为了节 ...

  8. Web 自动化测试框架 sweetest 介绍

    项目开源: https://github.com/tonglei100/sweetest 文章转载:https://segmentfault.com/a/1190000011612061 介绍 swe ...

  9. word2013密钥

    office 2013是一款功能强大的办公软件目前有两个版本分别为企业版和专业版,是继Microsoft Office 2010 后的新一代套装软件.2012年7月份,微软发布了免费的Office 2 ...

  10. Git-解释“Swap file .MERGE_MSG.swp already exists”的问题

    当合并代码时非正常保存退出遇到的问题. 博客原文: https://blog.csdn.net/qq_32452623/article/details/78395832