Date与时间戳的相互转换(Java)
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)的更多相关文章
- java中XMLGregorianCalendar类型和Date类型之间的相互转换
import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import ja ...
- java SimpleDateFormat日期与时间戳的相互转换
自我总结,有什么不到位的地方,各位可以帮忙纠正补充一下,感激不尽! 目的:SimpleDateFormat类可以很随意的组合日期时间的格式,不止单纯的yyyy-MM-dd这种格式 废话不多说,上代码 ...
- 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 ...
- java.util.Date日期类通过java语句转换成Sql(这里测试用的是oracle)语句可直接插入(如:insert into)的日期类型
public void add(Emp emp) throws Exception{ QueryRunner runner = new QueryRunner(JdbcUtil.getDataSour ...
- 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 ...
- Date与String的相互转换
构造函数 日期:new Date();//获取当前日期,精确到毫秒. 日期:new Date(long date);//即1970 年 1 月 1 日 00:00:00 GMT(Greenwich M ...
- Mysql时间戳转Java时间戳
MySQL 时间戳和Java返回的时间戳是不一样的 例如: 当前时间是 2014-08-04 10:42:55.204000 使用mysql时间戳函数UNIX_TIMESTAMP 返回的结果为: 14 ...
- java Date 当天时间戳处理
1. 代码 private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; private static Date ...
- java中时间与时间戳的相互转换
package com.test.one; import java.text.ParseException; import java.text.SimpleDateFormat; import jav ...
随机推荐
- 分析:java.lang.OutOfMemoryError: Java heap space
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Hashta ...
- Linux中计划任务执行脚本crontab-简洁版
我使用的是ubuntu16,所以在ubuntu中一切正常,在其他linux系统中应该都差不多. 1 计划任务,crontab命令选项: -u指定一个用户, -l列出某个用户的任务计划, ...
- mybatis-generator-core 自动生成实体和Mapper
所谓mybatis-generator-core就是利用mybatis-generator-core.jar自动生成数据库对应的实体和映射文件.首先要下载mybatis-generator-core- ...
- Entity Framework 学习
Entity Framework 学习初级篇1--EF基本概况 Entity Framework 学习初级篇2--ObjectContext.ObjectQuery.ObjectStateEntry. ...
- Lab 3-1
Analyze the malware found in the file Lab03-01.exe using basic dynamic analysis tools. Questions and ...
- webpack基础概念
中文文档:https://webpack.docschina.org/concepts/ 一个现代 JavaScript 应用程序的静态模块打包器(static module bundler). 在 ...
- 搭建智能合约开发环境Remix IDE及使用
目前开发智能的IDE, 首推还是Remix, 而Remix官网, 总是由于各种各样的(网络)原因无法使用,本文就来介绍一下如何在本地搭建智能合约开发环境remix-ide并介绍Remix的使用. 写在 ...
- SSD: ReLU6
1.在src\caffe\proto\caffe.proto中搜索message LayerParameter,在optional ReLUParameter relu_param = 123之后添加 ...
- 大div中嵌套小div,点击大div时隐藏,点击小div不隐藏
给小div添加一个click事件 <div onClick="event.cancelBubble = true"> //小div
- mysql存储引擎的对比