Java:Date与String类型的互转

news/2024/7/6 3:50:13 标签: SimpleDateFormat

SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。

根据上面的的“日期和时间模式”,设定需要匹配的模式,可以实现String与Date类型的互转,例如:

String类型的时间转换成Date类型时间,比较常用的几种时间格式转换如下:

a. 时间格式: “2015-08-28”, 模式: “yyyy-MM-dd”

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse("2015-08-28");
  
  • 1
  • 2

b. 时间格式: “2015-08-28 18:28:30”, 模式: “yyyy-MM-dd HH:mm:ss”

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse("2015-08-28 18:28:30");
  
  • 1
  • 2

c. 时间格式: “2015-8-28”, 模式: “yyyy-M-d”

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d");
Date date = dateFormat.parse("2015-8-28");
  
  • 1
  • 2

d. 时间格式: “2015-8-28 18:8:30”, 模式: “yyyy-M-d H:m:s”

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-d H:m:s");
Date date = dateFormat.parse("2015-8-28 18:8:30");
  
  • 1
  • 2

e. 时间格式: “Aug 28, 2015 6:8:30 PM”, 模式: “MMM d, yyyy h:m:s aa”

SimpleDateFormat dateFormat = new SimpleDateFormat("MMM d, yyyy h:m:s aa", Locale.ENGLISH);
Date date = dateFormat.parse("Aug 28, 2015 6:8:30 PM");
  
  • 1
  • 2

f. 时间格式: “Fri Aug 28 18:08:30 CST 2015”, 模式: “EEE MMM d HH:mm:ss ‘CST’ yyyy”

SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss 'CST' yyyy", Locale.ENGLISH);
Date date = dateFormat.parse("Fri Aug 28 18:08:30 CST 2015");
  
  • 1
  • 2

Date类型的时间转换成String类型时间

这是“String类型的时间转换成Date类型时间”的逆向操作,只要将Date date = dateFormat.parse([String型时间]);换成String date = dateFormat.format([Date型时间]);即可。例如,将当前时间格式化成[yyyy年M月d日]的形式:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年M月d日");
String date = dateFormat.format(new Date());
  
  • 1
  • 2

注:我们在做时间格式转换时,主要是找对匹配时间格式的模式;另外,英文格式的时间转换时需要带上Locale.ENGLISH,否则会转换失败,因为它默认的是本地化的设置,除非你的操作系统是英文的,总之时间转换时需要时间格式与模式保持一致。


http://www.niftyadmin.cn/n/869987.html

相关文章

java获取当前日期是星期几的简便方法

使用SimpleDateFormat格式化日期 Date datenew Date(); SimpleDateFormat dateFm new SimpleDateFormat("EEEE"); dateFm.format(date);注:格式化字符串存在区分大小写 对于创建SimpleDateFormat传入的参数:EEEE代表星期,如“星…

Serializable 序列化和反序列化的概念

转载于:https://blog.csdn.net/qq_27093465/article/details/78544505 遇到这个 Java Serializable 序列化这个接口,我们可能会有如下的问题a,什么叫序列化和反序列化 b,作用。为啥要实现这个 Serializable 接口,也就是…

[C++]MySQL数据库操作实例

由于课程大实验需要使用c操作MySQL数据库,经过一番研究终于成功实现VS2008中与MySQL的连接。 环境设置: 安装完MySQL之后,将安装目录中的include目录下的libmysql.lib文件拷到VS2008安装目录中的VC\lib\下,然后在 项目-选项-c/c-常…

mysql修改已存在的表增加ID属性为auto_increment自动增长的方法

http://www.2cto.com/database/201203/125731.html mysql修改已存在的表增加ID属性为auto_increment自动增长 今天有需要将已经存在表设置自动增长属性 具体如下 alter table customers change id id int not null auto_increment primary key; 扩展知识: //添加字…

ErrorCode:1068.Multipleprimarykeydefined

1、错误描述 ?110:10:38 alter table user add num int(8) primary key first Error Code: 1068. Multiple primary key defined 0.000 sec2、错误原因 这个错误的原因是定义了两个主键,导致出错 3、解决办法 alter table user add num int(8) primary key f…

C++ 操作MySql数据库实例讲解

学了这么久C,数据库访问部门一直没有多做了解,近来闲着,打算补上这一处的空白。于是果断的在Fedora 14上安装了数据库mysql,做测试之用。以下鄙人就将实验操作的过程总结一下,整理如下: 1) 安装…

如何对string使用sprintf() ?

整理于 http://bbs.csdn.net/topics/10325965 有少函数都需要char *参数,为的是通过指针直接写. 以前我们都这样写. char buf[256]; itoa(123, buf, 16); 但我不想再来一句string str buf; string.c_str()返回的是const char *, (LPTSTR)str也不行.

Matlab 中的线性规划函数使用方法

线性规划 LP(Linear programming,线性规划)是一种优化方法,在优化问题中目标函数和约束函数均为向量变量的线性函数,LP问题可描述为: min x s.t. Ax b Aeqxbeq vlb x vub 其中 ,b,beq均为向量,A,Aeq为…