(转)SimpleDateFormat使用
1 SimpleDateFormat
- public class SimpleDateFormat extends DateFormat
SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类。 它允许格式化 (date -> text)、语法分析 (text -> date)和标准化。
SimpleDateFormat 允许以为日期-时间格式化选择任何用户指定的方式启动。 但是,希望用 DateFormat 中的getTimeInstance、 getDateInstance 或 getDateTimeInstance 创建一个日期-时间格式化程序。 每个类方法返回一个以缺省格式化方式初始化的日期/时间格式化程序。 可以根据需要用 applyPattern 方法修改格式化方式。
SimpleDateFormat函数的继承关系:
java.lang.Object
|
+----java.text.Format
|
+----java.text.DateFormat
|
+----java.text.SimpleDateFormat
下面是个小例子:
import java.text.*;
import java.util.Date;
/**
SimpleDateFormat函数语法: G 年代标志符
y 年
M 月
d 日
h 时 在上午或下午 (1~12)
H 时 在一天中 (0~23)
m 分
s 秒
S 毫秒
E 星期
D 一年中的第几天
F 一月中第几个星期几
w 一年中第几个星期
W 一月中第几个星期
a 上午 / 下午 标记符
k 时 在一天中 (1~24)
K 时 在上午或下午 (0~11)
z 时区
*/
public class FormatDateTime {
public static void main(String[] args) {
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
SimpleDateFormat myFmt1=new SimpleDateFormat("yy/MM/dd HH:mm");
SimpleDateFormat myFmt2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//等价于now.toLocaleString()
SimpleDateFormat myFmt3=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
SimpleDateFormat myFmt4=new SimpleDateFormat(
"一年中的第 D 天 一年中第w个星期 一月中第W个星期 在一天中k时 z时区");
Date now=new Date();
System.out.println(myFmt.format(now));
System.out.println(myFmt1.format(now));
System.out.println(myFmt2.format(now));
System.out.println(myFmt3.format(now));
System.out.println(myFmt4.format(now));
System.out.println(now.toGMTString());
System.out.println(now.toLocaleString());
System.out.println(now.toString());
} }
运行结果:
2015年10月13日 09时51分14秒
15/10/13 09:51
2015-10-13 09:51:14
2015年10月13日 09时51分14秒 星期二
一年中的第 286 天 一年中第42个星期 一月中第3个星期 在一天中9时 CST时区
13 Oct 2015 01:51:14 GMT
2015-10-13 9:51:14
Tue Oct 13 09:51:14 CST 2015
ps:为什么有的格式大写,有的格式小写,那是怕避免混淆,例如MM是月份,mm是分;HH是24小时制,而hh是12小时制
下面是个JavaBean:
import java.text.SimpleDateFormat;
import java.util.Date; public class FormatDateTime { public static String toLongDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E ");
return myFmt.format(dt);
} public static String toShortDateString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy年MM月dd日 HH时mm分");
return myFmt.format(dt);
} public static String toLongTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("HH mm ss SSSS");
return myFmt.format(dt);
}
public static String toShortTimeString(Date dt){
SimpleDateFormat myFmt=new SimpleDateFormat("yy/MM/dd HH:mm");
return myFmt.format(dt);
} public static void main(String[] args) { Date now=new Date(); System.out.println(FormatDateTime.toLongDateString(now));
System.out.println(FormatDateTime.toShortDateString(now));
System.out.println(FormatDateTime.toLongTimeString(now));
System.out.println(FormatDateTime.toShortTimeString(now));
} }
调用的main 测试结果:
2015年10月13日 09时58分01秒 星期二
15年10月13日 09时58分
09 58 01 0787
15/10/13 09:58
SimpleDateFormat类的使用并不复杂,常用的方法在下面的例子就包含了:
import java.text.SimpleDateFormat;
import java.util.Date; public class Test
{
public static void main(String[] args)
{
//利用构造函数设置格式化模板
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
Date date = new Date();
//执行格式化功能
System.out.println(formatter.format(date));
//设置格式化模板
formatter.applyPattern("yyyy:MM:dd"); //yyyy-MM-dd
System.out.println(formatter.format(date));
}
}
2 时间格式转换
将yyyyMMddhhmmss(20140707103709)转换为yyyy-MM-dd HH:mm:ss
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = (Date) sdf1.parse(s);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.print(sdf2.format(date));
用于String和Date之间相互转换
(转)SimpleDateFormat使用的更多相关文章
- SimpleDateFormat使用详解——日期、字符串应用
public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...
- SimpleDateFormat转换时间格式
SimpleDateFormat有两个常用的方法parse和format 其中SimpleDateFormat在创建时有一下集中格式可以选择 SimpleDateFormat sdf = new Si ...
- DateUtil(SimpleDateFormat)
import java.util.Calendar; import java.util.Date; import java.text.SimpleDateFormat; public class Da ...
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
java日期格式大全 format SimpleDateFormat(转) SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH ...
- java 中的SimpleDateFormat、Date函数以及字符串和Date类型互转
SimpleDateFormat是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本).解析(文本 -> 日期)和规范化. SimpleDateFor ...
- java日期处理SimpleDateFormat等
1.mysql数据库中有这样一个表: mysql> select * from test_table;+----------+---------------------+| username | ...
- SimpleDateFormat的应用
import java.text.SimpleDateFormat;import java.util.Date;public class Main { public static void ma ...
- 使用 Date 和 SimpleDateFormat 类表示时间、Calendar类和Math类
一. Date 和 SimpleDateFormat类表示时间 在程序开发中,经常需要处理日期和时间的相关数据,此时我们可以使用 java.util 包中的 Date 类.这个类最主要的作用就是获取当 ...
- Java SimpleDateFormat使用
SimpleDateFormat 是一个格式化日期的具体类. 它允许格式化 (date -> text) 和标准化. SimpleDateFormat 允许以为日期-时间格式化选择任何用户指定的 ...
- SimpleDateFormat 12小时制以及24小时制的写法
有些代码按了复制键没有效果,但是其实已经复制到剪贴板上面了,大家请注意哦! 我的文章有时会稍有修改,转载请注明出处哦! 原文地址:SimpleDateFormat 12小时制以及24小时制的写法 去代 ...
随机推荐
- springMVC实现REST开发详解(补充Json解析问题以及静态文件404错误解决办法)
一.什么是REST? 符合REST约束风格和原则的应用程序或者设计就是REST 例如: /blog/1 HTTP GET =>查询id=1的blog /blog/1 HTTP DE ...
- python爬虫番外篇(一)进程,线程的初步了解
一.进程 程序并不能单独和运行只有将程序装载到内存中,系统为他分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别在于:程序是指令的集合,它是进程的静态描述文本:进程是程序的一次执行活动, ...
- js移动端/H5同时选择多张图片上传并使用canvas压缩图片
最近在做一个H5的项目,里边涉及到拍照上传图片的功能以及识别图片的功能,这里对识别图片的功能不做赘述,不属本文范畴.我在做完并上线项目后,同事跟我提了一个要求是可不可以同时选择多张图片上传,我做的时候 ...
- jQuery时间日期插件laydate,兼容bootstrap
解压后,将laydate整个文件放至您项目的任意目录,不要移动其文件结构,它们具有完整的依赖体系. 使用只需在页面引入laydate.js即可. 如果您的网站的js采用合并或模块加载,您需要打开lay ...
- Swift3 访问权限fileprivate和 open
在swift 3中新增加了两种访问控制权限 fileprivate和 open. 下面结合网上资料和个人理解整理一下两个属性的原理与介绍. fileprivate 在原有的swift中的 privat ...
- mysql update 批量更新
UPDATE cntheater SET title = (SELECT title FROM cntheater_copy WHERE cntheater.id = cntheater_copy.i ...
- windows下pycharm远程调试pyspark
参考http://www.mamicode.com/info-detail-1523356.html1.远端执行:vi /etc/profile添加一行:PYTHONPATH=$SPARK_HOME/ ...
- 移动端布局,C3新增属性
<html5拖拽> 1.给元素设置 draggable="true" 属性,这个元素就可以被拖拽了 <拖拽元素事件> 2.ondragstart 拖拽前触发 ...
- MAC开发环境安装
MAC开发环境安装 安装sancha cmd: 安装: https://www.sencha.com/products/extjs/cmd-download/ cmd运行 $ open .bash_p ...
- tcp/ip通信传输流
利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信,发送端从应用层往下走,接收端则往应用层方向走. 我们用HTTP进行举例 客户端在应用层发出想要看到某个web页面的http请求.HT ...