java timestamp转换date_Java中Timestamp类型如何转成Date类型
记录一下:今天在项目中遇到了类型是Timestamp的数据,在前台页面显示时不是我们习惯的常规时间显示格式,如下:
调整前台的时间显示,有两种思路:在前台js去调整和后台处理后再发送到前台展示。
这里选择了在后台先进行数据格式的处理,再发送到前台(主要前端不熟)
转换的代码如下:
for (int i=0;i
Date dates = new Date();
try {
Timestamp ts = new Timestamp(inverstList.get(i).getUpdateTime().getTime());
dates = ts;
}catch (Exception e){
e.printStackTrace();
}
inverstList.get(i).setUpdateTime(dates);
}
for (int i=0;i
Date dates = new Date();
try {
Timestamp ts = new Timestamp(inverstList.get(i).getUpdateTime().getTime());
dates = ts;
}catch (Exception e){
e.printStackTrace();
}
inverstList.get(i).setUpdateTime(dates);
}
这里需要注意的是,很多库提供了Timestamp,但是这里引入的包是:
import java.sql.Timestamp;
import java.sql.Timestamp;
扩展:
String 类型转Date类型
String dateStr = "2010/05/04 12:34:23";
Date date = new Date();
//注意format的格式要与日期String的格式相匹配
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
date = sdf.parse(dateStr);
System.out.println(date.toString());
} catch (Exception e) {
e.printStackTrace();
}
String dateStr = "2010/05/04 12:34:23";
Date date = new Date();
//注意format的格式要与日期String的格式相匹配
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
date = sdf.parse(dateStr);
System.out.println(date.toString());
} catch (Exception e) {
e.printStackTrace();
}
Date 类型转String类型
String dateStr = "";
Date date = new Date();
//format的格式可以任意
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
try {
dateStr = sdf.format(date);
System.out.println(dateStr);
dateStr = sdf2.format(date);
System.out.println(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
String dateStr = "";
Date date = new Date();
//format的格式可以任意
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
try {
dateStr = sdf.format(date);
System.out.println(dateStr);
dateStr = sdf2.format(date);
System.out.println(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
String 类型转Timestamp类型
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "2011-05-09 11:49:45";
try {
ts = Timestamp.valueOf(tsStr);
System.out.println(ts);
} catch (Exception e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "2011-05-09 11:49:45";
try {
ts = Timestamp.valueOf(tsStr);
System.out.println(ts);
} catch (Exception e) {
e.printStackTrace();
}
Timestamp 类型转String类型
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "";
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
//方法一
tsStr = sdf.format(ts);
System.out.println(tsStr);
//方法二
tsStr = ts.toString();
System.out.println(tsStr);
} catch (Exception e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsStr = "";
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
try {
//方法一
tsStr = sdf.format(ts);
System.out.println(tsStr);
//方法二
tsStr = ts.toString();
System.out.println(tsStr);
} catch (Exception e) {
e.printStackTrace();
}
本文用到的,Timestamp 转 Date
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date();
try {
date = ts;
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
}
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date();
try {
date = ts;
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
}
java timestamp转换date_Java中Timestamp类型如何转成Date类型相关推荐
- 【spring boot】Controller @RequestMapping 数据绑定:接收 Date 类型参数时遇错,将 String 类型的参数转换成 Date 类型
前言 spring boot 2.1.1.RELEASE 遇错 Failed to convert value of type 'java.lang.String' to required type ...
- oracle date 转换 timestamp,Oracle timestamp类型转换成date类型
今天需要根据时间判断,统一修改某一个字段的数据.然后打开数据库发现,时间类型为timestamp类型.如下: 然后呢,这对我不是喝口水就可以解决的问题吗? 解决方案如下:我需要改这张表某个字段的内容, ...
- 字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: “2019-09-27T18:31:31+08:00”
字符串类型日期时间转换为Date类型解析转换异常java.text.ParseException: Unparseable date: "2019-09-27T18:31:31+08:00& ...
- java获取系统date_Java中获取当前时间
一. 获取当前系统时间和日期并格式化输出: import java.util.Date; import java.text.SimpleDateFormat; public class NowStri ...
- SQL Server 2008 Datetime Cast 成 Date 类型可以使用索引(转载)
很久没写blog,不是懒,实在是最近我这的访问速度不好,用firefox经常上传不了图片 ....... 今天无意发现了SQL Server 2008 Datetime Cast 成 Date 类型可 ...
- java calendar与date_Java中date和calendar的用法
获取现在系统的时间和日期看起来是一件非常神奇的事情,但是当使用date和calendar之后发现仍然非常神奇. 1.date 使用date日期之前需要导入包: import java.text.Sim ...
- java form的时间格式_SpringMvc接收日期表单提交,自动转换成Date类型方法
User中有birthday(Date)属性,用户注册的时候,选择日期即可,然后提交表单,可spring mvc 报错,400 Bad Request意思是不能把字符串转为Date类型的. 实体类中加 ...
- string转换成date类型_你竟然知道SpringMVC是如何完成类型转换和数据绑定的吗?...
# 前言 SpringMVC是目前主流的Web MVC框架之一. public String method(Integer num, Date birth) { ...} Http请求传递的数据都是字 ...
- ElementUI中el-upload传递额外参数为date类型时后台SpringBoot接收不到
场景 ElementUI中el-upload怎样上传文件并且传递额外参数给Springboot后台进行接收: https://mp.csdn.net/console/editor/html/10797 ...
最新文章
- ASP.NET服务器控件的生命周期分析
- QT的QScriptValueIterator类的使用
- 如何使用PHP中的字符串函数
- PCB画板子常用快捷键总结
- 转载在linux下执行java窗口被锁定问题
- SQL Server查询存储–概述
- java中的上传下载----ajaxFileUpload+struts2
- 火车票售票系统需求分析
- 【FPGA】QuartusII_13.1安装及破解
- Word2016怎么插入页眉页脚?Word2016页眉页脚添加教程
- 北京16个山水美景| 清明小长假走起吧!
- CSS3 Transition
- TypeError: ufunc ‘isnan‘ not supported for the input types, and the inputs could not be...可能的原因记录
- C题——Halting Problem(补题)
- 隐马尔可夫模型的三个基本问题(例子说明)
- 资源优化问题常见形式
- 《花千骨》为何被批“脑残”还能创造收视神话?
- 我们的空间是它圆——基于Poicare对宇宙的模型
- Java系列技术之SSH整合+用户权限控制模型项目-钟洪发-专题视频课程
- sm2多端加密解密,java,js,android,ios实战