SIMPLEDATEFORMAT function example

xiaoxiao2021-03-06  47

Public class

SimpleDateFormat Extends DateFormat

SimpleDateFormat is a specific class that formats and analyzes data in country-sensitive ways. It allows formatted (DATE -> Text), syntax analysis (Text -> Date) and standardization.

SimpleDateFormat allows for the date-time formatting to select any user-specified way to start. However, I hope to create a date-time formatting program with GetTimeInstance, GetDateInstance or GetDateTimeInstance in DateFormat. Each class method returns a date / time formatting program initialized by default formatting. The formatted method can be modified using the ApplyPattern method as needed. SIMPLEDATEFORMAT function inheritance: java.lang.object | ---- java.text.format | ---- java.text.dateFormat | ---- java.text.SIMPLEDATEFORMAT below is a small example: import Java.text. *; import java.util.date;

/ ** SIMPLEDATEFORMAT function syntax: g age marker Y year M Month D day H in the morning or afternoon (1 ~ 12) h in the day (0 ~ 23) M points s second S millisecond E Week D During the year The first few days in the first month of the first month of the first month of the first week in the first month, the first day of the morning a month AM, in the afternoon, in the morning (1 ~ 24) k, in the morning or Afternoon (0 ~ 11) z time zone * / public class formatdatetime {

Public static void main (String [] args) {SimpleDateFormat Myfmt = new SimpleDateFormat ("YYY YYY MM Moon DD Day HH MM] SS Second"); SimpleDateFormat Myfmt1 = New SimpleDateFormat ("YY / mm / dd hh: mm") SimpleDateformat myfmt2 = new simpledateformat ("YYY-MM-DD HH: MM: SS"); // Is equivalent to now.tolocalestring () SimpleDateFormat myfmt3 = new SimpleDateFormat ("YYYY MM Month DD date HH MM points SS seconds E "); SimpleDateFormat myfmt4 = new SimpleDateFormat (" X-time Z time zone in the first day of the first week in the first year in the year "); Date Now = new date (); System.out.println; system.out.println; system.out.println (Myfmt2.Format (now)); System.out.Println (MyFMT3. Format (now); system.out.println; system.out.println (now.togmtstring ()); system.out.println; system.out. Println () (now.toString ());}} Effect: December 16, 2004 17:24 27 seconds 04/12-16 17: 24: 27 December 16, 2004 17:24 The 51st weeks of the 351st week of Thursday, 27 seconds The 3rd week of January 17 o'clock in the day, 17:24 09:24:27 GMT2004-12-16 17: 24: 27thu DEC 16 17:24:27 CST 2004 below is a javabean: public class formatdatetime {public class formatdatetime {public static String toLongDateString (date dt) {SimpleDateFormat myFmt = new SimpleDateFormat ( "dd day when HH yyyy, mM month mm ss a E"); return myFmt.format (dt);} public static String toShortDateString (date dt) {SimpleDateFormat Myfmt = new SimpleDateFormat ("YY year mm month DD day hh MM points"); 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. ToshostTimeString (now);}} Call MAIN test results: December 16, 2004 17:38 26 second Thursday, December 16, 2004, 17:38:17 38:38

转载请注明原文地址:https://www.9cbs.com/read-79981.html

New Post(0)