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());
}
}
效果:
2004年12月16日 17时24分27秒
04/12/16 17:24
2004-12-16 17:24:27
2004年12月16日 17时24分27秒 星期四
一年中的第 351 天 一年中第51个星期 一月中第3个星期 在一天中17时 CST时区
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
下面是个JavaBean:
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 测试结果:
2004年12月16日 17时38分26秒 星期四
04年12月16日 17时38分
17 38 26 0965
04/12/16 17:38
2. java SimpleDateFormat 英文格式
//2012-01-16
System.out.println((new SimpleDateFormat("yyyy-MM-dd")).format(new Date()));
//16-Jan-2012
System.out.println((new SimpleDateFormat("dd-MMM-yyyy",Locale.ENGLISH)).format(new Date()));
//16-Jan-2012 Monday
System.out.println((new SimpleDateFormat("dd-MMM-yyyy EEEEE",Locale.ENGLISH)).format(new Date()));
//16-Jan-2012 Monday PM
System.out.println((new SimpleDateFormat("dd-MMM-yyyy EEEEE aa",Locale.ENGLISH)).format(new Date()));
3. 与毫秒的相互转换
public static void main(String argv[]){ String str = "2015 Aug 22 00:37:53.305";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss.SSS",Locale.ENGLISH);
long millionSeconds = 0;
try {
millionSeconds = sdf.parse(str).getTime();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//毫秒
System.out.println(millionSeconds);
Date date = new Date(millionSeconds);
System.out.println(sdf.format(date));
}
输出结果:
1440175073305
2015 Aug 22 00:37:53.305
4. 其它参考链接
SimpleDateFormat 参数
SimpleDateFormat使用详解及与毫秒的相互转换的更多相关文章
- SimpleDateFormat使用详解——日期、字符串应用
public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...
- SimpleDateFormat使用详解
http://blog.csdn.net/gubaohua/article/details/575488 public class SimpleDateFormat extends DateForma ...
- (转)SimpleDateFormat使用详解
1 SimpleDateFormat 介绍 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格 ...
- SimpleDateFormat使用详解 <转>
public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...
- Java中SimpleDateFormat用法详解
所有已实现的接口: Serializable, Cloneable SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的具体类.它允许进行格式化(日期 -> 文本) ...
- DateFormat与SimpleDateFormat区别和使用详解
DateFormat类 此类是一个日期的格式化类,用来格式化日期.具体日期可以通过java.util.Date类来获取. DateFormat类的定义:此类是定义在java.test包中的. publ ...
- SimpleDataFormat详解
[转]SimpleDateFormat使用详解 public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方 ...
- android中getSystemService详解
android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...
- 图像处理之基础---yuv420及其rgb,bayer, yuv, RGB的相互转换详解
YUV格式解析1(播放器——project2) 根据板卡api设计实现yuv420格式的视频播放器 打开*.mp4;*.264类型的文件,实现其播放. 使用的视频格式是YUV420格式 YUV格式 ...
随机推荐
- 从 Page not found: / 提示说起,我是怎么发现webstrom与myeclipse冲突问题及解决的
#从 Page not found: / 提示说起,我是怎么发现webstrom与myeclipse冲突问题的 ## 从前面发表了两篇博文,[webstorm+nodejs+JetBrains ID ...
- 去掉StringBuilder或String 最后一个项逗号
一. sb.Length = sb.Length - 1; 二. stringBuilder.Remove(stringBuilder.ToString().LastIndexOf(','), 1); ...
- 麒麟OS剽窃
今年对于我们的IT行业来说可以算是耻辱的一年. 首先是“汉芯丑闻”,上海交大研制了一个所谓的国内第一个完全拥有自主知识产 权的DSP芯片(数字信号微处理器)——“汉芯”,研制人陈进教授以此领取政府一亿 ...
- c++builder XE8 线程 Thread
thread Thread c++builder XE8 / RAD 10 Settle delphi TThread.CreateAnonymousThread(MyMethod).Start; ...
- 由于SSH配置文件的不匹配,导致的Permission denied (publickey)及其解决方法。
读者如要转载,请标明出处和作者名,谢谢.地址01:http://space.itpub.net/25851087地址02:http://www.cnblogs.com/zjrodger/作者名:zjr ...
- Gradle 1.3之前的Publishing artifacts
在Gradle1.3之前,Publishing artifacts是使用uploadConfigurationName来publish 声明artifacts是靠使用 build.gradle art ...
- postconf 命令常用参数
postfix的main.cf配置文件一般不直接编辑,而多使用postconf命令来配置‘ postconf -d:查看默认配置: postconf -n:查看当前配置(即当前生效的配置): post ...
- win7 文件共享 xp
前几天因为需要将win7内一文件夹共享给XP使用,因为NT5跟NT6安全机制的问题,共享的实现没有XP共享的方便,很多人是牺牲(关闭)了win7的系统防火墙才达到共享给XP的目的,但是关闭防火墙势必会 ...
- windows服务安装及卸载
1)安装脚本Install.bat%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe JobSchedule.exeNet ...
- iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像
iOS面向编码|iOSVideoToolbox:读写解码回调函数CVImageBufferRef的YUV图像 本文档基于H.264的解码,介绍读写Video Toolbox解码回调函数参数CVImag ...