simpledateformat是處理日期格式轉換的類。
官方api_1.8關于simpledateformat繼承于dateformate截圖:
simpledateformat的構造器如下:
simpledateformat中的格式定義,常用的用紅色框圈出:
中文解釋:
y : 年
m : 年中的月份
d : 年中的天數
d : 月中的天數
w : 年中的周數
w : 月中的周數
a : 上下/下午
h : 一天中的小時數(0-23)
h : 一天中的小時數(0-12)
m : 小時中的分鐘
s : 分鐘中的秒數
s : 毫秒數
simpledateformat方法:
繼承于dateformate的方法:
simpledateformat常用方法和常用格式定義使用實例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package com.lanhuigu.java.format; import java.text.parseexception; import java.text.simpledateformat; import java.util.date; public class formattest { public static void main(string[] args) throws parseexception { // **************1.(format,parse)最常用方法實例************* system.out.println( "----------最常用方法---------" ); // 格式 simpledateformat sdf1 = new simpledateformat( "yyyy-mm-dd hh:mm:ss" ); // 時間 date date1 = new date(); system.out.println( "操作前的時間:" + date1); // 日期類型時間-》轉換為定義格式-》字符串類型時間 /* * 注意: format(date date)這個方法來自于simpledateformat的父類dateformat */ string str1 = sdf1.format(date1); system.out.println("字符串類型時間:" + str1); // 字符串類型時間-》轉換為定義格式-》日期類型時間 date datef1 = sdf1.parse(str1); system.out.println("日期類型時間:" + datef1); // **************2.關于常用格式分析************* system.out.println("----------常用格式分析---------"); /* * y : 年 * m : 年中的月份 * d : 年中的天數 * d : 月中的天數 * w : 年中的周數 * w : 月中的周數 * a : 上下/下午 * h : 一天中的小時數(0-23) * h : 一天中的小時數(0-12) * m : 小時中的分鐘 * s : 分鐘鐘的秒數 * s : 毫秒數 */ // 注意,為了省事,這個地方把常用的都放進來了,一起打印看效果, // 在實際使用中,根據需求進行相應格式轉換 simpledateformat sdf2 = new simpledateformat("yyyy-mm-dd,w,w,a,hh:mm:ss,ss"); string str2 = sdf2.format(new date()); system.out.println("日期類型時間:" + str2); system.out.println("字符串類型時間:" + sdf2.parse(str2)); // **************2.關于構造器使用技巧分析************* system.out.println("----------構造器使用技巧分析---------"); /* * 構造器: * simpledateformat(); * simpledateformat(string pattern); * simpledateformat(string pattern, dateformatsymbols formatsymbols); * simpledateformat(string pattern, locale locale) */ // 通過對應構造器構造對象,直接調用方法,簡潔寫法 system.out.println( new simpledateformat( "yyyy-mm-dd hh:mm:ss" ).format( new date())); } } |
程序運行結果:
總結
關于simpledateformate需會使用其不同參數下的常用方法,以及常用格式,構造器簡寫方式。
以上就是本文全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/yhl_jxy/article/details/53424717