SimpleDateFormat is a concrete class for formatting and analyzing data in a country-sensitive manner. This article mainly introduces the detailed use of SimpleDateFormat in Java. Friends in need can refer to
public class SimpleDateFormat extends DateFormat
SimpleDateFormat is a specific class that formats and analyzes data in a country-sensitive manner. It allows formatting (date -> text), parsing (text -> date) and normalization.
SimpleDateFormat allows the selection of any user-specified method for date-time formatting. However, you want to create a date-time formatter using getTimeInstance, getDateInstance, or getDateTimeInstance from DateFormat. Each class method returns a date/time formatter initialized with default formatting. You can use the applyPattern method to modify the formatting method as needed.
SimpleDateFormatInheritance of functionRelationship:
Java.lang.Object | +----java.text.Format | +----java.text.DateFormat | +----java.text.SimpleDateFormat
The following is a small example:
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()); } }
Effect:
December 16, 2004 17:24:27
04/12/16 17:24
2004-12-16 17:24:27
December 16, 2004 17:24 27 seconds Thursday
The 351st day of the year The 51st Monday of the year The 3rd week of the month At 17:00 CST time zone of the day
16 Dec 2004 09:24:27 GMT
2004-12-16 17:24:27
Thu Dec 16 17:24:27 CST 2004
The following is a 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)); } }
The main test result called:
Thursday, December 16, 2004 17:38:26
December 16, 2004 17:38
17 38 26 0965
04/12/16 17:38
The above is the detailed content of Detailed explanation of the sample code used by SimpleDateFormat in Java. For more information, please follow other related articles on the PHP Chinese website!