SimpleDateFormat 的 format 方法使用具体解释

Java中怎么才干把日期转换成想要的格式呢。或把字符串转换成一定格式的日期,如把数据库中的日期或时间转换成自己想要的格式,JAVA中提供了SimpleDateFormat类能够实现,下面是SimpleDateFormat的使用方法及实例:

java.lang.Object

java.text.Format

java.text.DateFormat

java.text.SimpleDateFormat

全部已实现的接口:

Serializable, Cloneable

SimpleDateFormat 是一个以与语言环境有关的方式来格式化和解析日期的详细类。

它同意进行格式化(日期 -> 文本)、解析(文本 -> 日期)和规范化。

SimpleDateFormat 使得可以选择不论什么用户定义的日期-时间格式的模式。可是,仍然建议通过 DateFormat 中的 getTimeInstance、getDateInstance 或 getDateTimeInstance 来创建日期-时间格式器。每个这种类方法都可以返回一个以默认格式模式初始化的日期/时间格式器。

package org.bupt.xiaoye.chapter3;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatTest {

	public static void main(String[] args) throws ParseException {
		SimpleDateFormat CeshiFmt0 = new SimpleDateFormat(
				"Gyyyy年MM月dd日 HH时mm分ss秒");
		SimpleDateFormat CeshiFmt1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		SimpleDateFormat CeshiFmt2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat CeshiFmt3 = new SimpleDateFormat(
				"yyyy年MM月dd日 HH时mm分ss秒 E ");
		SimpleDateFormat CeshiFmt4 = new SimpleDateFormat("yyyy/MM/dd E");
		SimpleDateFormat CeshiFmt5 = new SimpleDateFormat(
				"一年中的第 D 天 ,第w个星期 ,一个月中第W个星期 ,k时 z时区");
		Date now = new Date();
		System.out.println(CeshiFmt0.format(now));
		System.out.println(CeshiFmt1.format(now));
		System.out.println(CeshiFmt2.format(now));
		System.out.println(CeshiFmt3.format(now));
		System.out.println(CeshiFmt4.format(now));
		System.out.println(CeshiFmt5.format(now));
	}

}

执行结果:

公元2014年10月19日 18时08分08秒

2014/10/19 18:08

2014-10-19 18:08:08

2014年10月19日 18时08分08秒 星期日

2014/10/19 星期日

一年中的第 292 天 。第43个星期 ,一个月中第4个星期 ,18时 CST时区

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 时区

时间: 2024-07-30 01:17:13

SimpleDateFormat 的 format 方法使用具体解释的相关文章

SimpleDateFormat 的 format 方法使用详解

Java中怎么才能把日期转换成想要的格式呢,或把字符串转换成一定格式的日期,如把数据库中的日期或时间转换成自己想要的格式,JAVA中提供了SimpleDateFormat类可以实现,以下是SimpleDateFormat的用法及实例: java.lang.Object java.text.Format java.text.DateFormat java.text.SimpleDateFormat 所有已实现的接口: Serializable, Cloneable SimpleDateFormat

Date类与SimpleDateFormat类中parse()方法和format()方法

1 package ppt11util类; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 public class SimpleDateFormat类 { 8 public static void main(String[] args) throws ParseException { 9 Date date =new Date();//获

DateFormat的format()方法线程不安全的问题分析

最近看到<侦探剧场:堆内存神秘溢出事件>https://my.oschina.net/u/2368090/blog/1628720,于是自己也想测试了解一下DateFormat的多线程安全问题. 首先先看现象:做一个多线程使用同一个SimplyDateFormat的测试例子: import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatTest { static SimpleDateForma

30 字符串的基本操作 格式化字符串(%,Template类,format方法*****)

Python视频课程(5)-Python字符串 第一课 字符串的基本操作 # 字符串:基本操作 字符串取单个字母 s1 = "I love python." print(s1[7]) # p print(s1[11]) # o # print(s1[15]) # 超过字符串长度 会报错 # 利用分片截取字符串的子字符串 取一段区间的字符串 print(s1[7:13]) # python print(s1[7:]) # python. print(s1[::2]) # Ilv yhn

String.format()方法使用说明

JDK1.5开始String类中提供了一个非常有用的方法String.format(String format, Object ... args) 查看源码得知其实是调用了Java.util.Formatter.format(String, Object...)方法 [java] view plain copy print? public static String format(String format, Object ... args) { return new Formatter().f

测试String.Format方法

今天想使用String.Format,和平时的用法不一样. 直接上代码: [Test] public void TestMethod6() { string A = "A"; string B = "B"; string C = "C"; string D = "{0}"; String str = String.Format(D, A, B, C); Assert.AreEqual(str, "A");

C#语言基础知识(4):C#中的String.Format方法

定义String.Format是将指定的String类型的数据中的每个格式项替换为相应对象的值的文本等效项.(1)string p1="xiaomeng";string p2="xiaobei";Response.Write(String.Format("Hello {0},I'm {1}",p1,p1));(2)Response.Write(String.Format("Hello {0},I'm {1}","xi

前段 format方法

a.为字符串创建format方法,用于字符串格式化 String.prototype.format=function (arg) { //console.log(this,arg); //this,当前调用方法的字符串,arg为Format方法传入的参数 //return '666'; //return,格式化之后获取的新内容,return啥就替换为啥 var temp = this.replace(/\{(\w+)\}/g,function (k,kk) { // k相当于{(\w+)},kk

python的str.format方法

format方法被用于字符串的格式化输出. print('{0}+{1}={2}'.format(1,2,1+2)) #in 1+2=3 #out 可见字符串中大括号内的数字分别对应着format的几个参数. 若省略数字: print('{}+{}={}'.format(1,2,1+2)) #in 可以得到同样的输出结果.但是替换顺序默认按照[0],[1],[2]...进行. 若替换{0}和{1}: print('{1}+{0}={2}'.format(1,2,1+2)) #in 2+1=3 #