Java SimpleDateFormat 中英文时间格式化转换
SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类。它允许进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。
SimpleDateFormat使得可以选择任何用户定义的日期-时间格式的模式。但是,仍然建议通过DateFormat中的getTimeInstance、getDateInstance 或 getDateTimeInstance 来创建日期-时间格式器。每一个这样的类方法都能够返回一个以默认格式模式初始化的日期/时间格式器。可以根据需要使用applyPattern 方法来修改格式模式。
日期和时间模式
| 字母 | 日期或时间元素 | 表示 | 示例 |
|---|---|---|---|
| G | Era | 标志符 | Text |
| y | 年 | Year | 1996; 96 |
| M | 年中的月份 | Month | July; Jul; 07 |
| w | 年中的周数 | Number | 27 |
| W | 月份中的周数 | Number | 2 |
| D | 年中的天数 | Number | 189 |
| d | 月份中的天数 | Number | 10 |
| F | 月份中的星期 | Number | 2 |
| E | 星期中的天数 | Text | Tuesday; Tue |
| a | am/pm 标记 | Text | PM |
| H | 一天中的小时数(0-23) | Number | 0 |
| k | 一天中的小时数(1-24) | Number | 24 |
| K | am/pm 中的小时数(0-11) | Number | 0 |
| h | am/pm 中的小时数(1-12) | Number | 12 |
| m | 小时中的分钟数 | Number | 30 |
| s | 分钟中的秒数 | Number | 55 |
| S | 毫秒数 | Number | 978 |
| z | 时区 | General time zone | Pacific Standard Time; PST; GMT-08:00 |
| Z | 时区 | RFC 822 time zone | -0800 |
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,否则会转换失败,因为它默认的是本地化的设置,除非你的操作系统是英文的,总之时间转换时需要时间格式与模式保持一致。
Java SimpleDateFormat 中英文时间格式化转换的更多相关文章
- Java之格林威治时间格式转换成北京时间格式
Java之格林威治时间格式转换成北京时间格式 package com.mtons.mblog; import java.text.ParseException; import java.text.Si ...
- Java和数据库时间格式化格式
JAVA: yyyy-MM-dd HH-mm-ss(大小HH表示24小时制); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd&q ...
- Java date format 时间格式化
import java.util.Date; import java.text.DateFormat; /** * 格式化时间类 * DateFormat.FULL = 0 * DateForma ...
- java 和js 时间 格式化(yyyy-MM-dd HH:mm:ss) 以及获取当前时间
1.js var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970 ...
- jS处理中英文时间格式化问题
// datebox里面日期格式化,i18nStr是当前国际化的日期格式,fdate是需要处理的时间,默认为当前时间 function formatDate(i18nStr,fdate) { var ...
- Java SimpleDateFormat 日期-时间格式参数
字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month ...
- Java ISO 8601时间格式转换
common-lang包: String pattern = "YYYY-MM-dd'T'HH:mm:ssZZ"; System.out.println(DateFormatUti ...
- Java SimpleDateFormat处理日期与字符串的转换
1.为什么要使用SimpleDateFormat? 在Java中,如果我们想获取当前时间,一般会使用Date类的无参构造函数,如下所示,我们获取到当前时间并输出: import java.util.D ...
- Java 日期时间格式化
在此记录Java日期时间格式化转换符,方便以后有需要时查找. 1.日期格式化 2.时间格式化 3.格式化常见的日期时间组合
随机推荐
- Oracle 服务器运行健康状况监控利器 Spotlight on Oracle 的安装与使用
1.使用教程;https://blog.csdn.net/defonds/article/details/52936664 2.下载链接:https://pan.baidu.com/s/1cn7tE_ ...
- AviSynth AVS Importer Plugin for Adobe Premiere Pro CC 2015 x64
Premiere CS AVS Importer x64.prm copy to Adobe\Adobe Premiere Pro CC 2015\Plug-Ins\Common\ VSFilterM ...
- ubuntu 安装 wireshark
1.添加 wireshark 源 sudo apt-add-repository ppa:wireshark-dev/stable 2.更新 sudo apt-get update 3.安装 w ...
- pwnable.kr simple login writeup
这道题是pwnable.kr Rookiss部分的simple login,需要我们去覆盖程序的ebp,eip,esp去改变程序的执行流程 主要逻辑是输入一个字符串,base64解码后看是否与题目 ...
- 解决mysql 主从数据库同步不一致的方法
接着上文 配置完Mysql 主从之后,在使用中可能会出现主从同步失败的情况. mysql> show slave status\G Slave_IO_Running: Yes Slave_SQL ...
- 海康JAVA SDK库动态路径加载
海康JAVA SDK初始化路径默认是放在classes下面,见下: HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary("HCNetSDK&q ...
- Vuex与axios介绍
Vuex集中式状态管理里架构 axios (Ajax) Vuex集中式状态管理架构 -简单介绍: vuex是一个专门为Vue.js设计的集中式状态管理架构. 我们把它理解为在data中需要共享给其他组 ...
- 行为驱动:BDD框架之Cucumber初探
1.cucumber cucumber早在ruby环境下应用广泛,作为BDD框架的先驱,cucumber后来被移植到了多平台,简单来说cucumber是一个测试框架,就像是juint或是rspec一样 ...
- python字符串str和字节数组bytes相互转化
1 引言 后续待补充 2 代码 b = b"Hello, world!" # bytes s = "Hello, world!" # string print( ...
- 洛谷P4630 [APIO2018]铁人两项 [广义圆方树]
传送门 又学会了一个新东西好开心呢~ 思路 显然,假如枚举了起始点\(x\)和终止点\(y\),中转点就必须在它们之间的简单路径上. 不知为何想到了圆方树,可以发现,如果把方点的权值记为双联通分量的大 ...