String.Format () "Example in detail"

Source: Internet
Author: User
Tags rfc822

String.Format () "Example in detail"


Finishing by: Vashon

Objective:

String.FormatAs a text processing tool, we provide a powerful and rich string formatting function, in order not to stop the simple callString.Format ("Hello%s", "Vashon");

, keep your notes organized and documented below.

Method overloads:

// format a string using the current local region object (Locale.getdefault ()) string String.Format (String fmt, Object ... args); // To customize a local Area object formatting string String String.Format (locale locale, String fmt, Object ... args);

An example is detailed:

Import Java.util.date;import Java.util.Locale; Import Org.junit.Test; public class StringFormat {/**string.format () Usage 1, translator%s: String type, such as: "HQL"%b: Boolean type, such as: true%d: integer type (decimal), such as: 99%f: Floating-point type,    such as: 99.99%: Percent type, such as:%%n: newline character */@Testpublic void test1 () {String str=null; Str=string.format ("Hi,%s", "Mr. Yang"); Formatted string System.out.println (str);    Output string variable str content System.out.printf ("3>7 result is:%b%n", 3>7);    System.out.printf ("Half of 100 is:%d%n", 100/2);    System.out.printf ("50 yuan book 8.5 discount is:%f Yuan%n", 50*0.85); System.out.printf ("The above discount is%d%%%n", 85);} /*2, Common datetime formatting C: Includes all date and time information week Liuxing 14:21:20 CST 2007F: "Year-month-day" format, such as: 2007-10-27d: "Month/day/year" format, such as: 10/27/07r: "HH:MM:SS PM" format (12 o'clock), such as: 02:25:51 PM T: "HH:MM:SS" format (24 o'clock), such as: 14:28:16r: "hh:mm" format (24 o'clock), such as: 14:28 */@Testpublic void Test2 () {Date date=n EW Date ();    Create Date Object System.out.printf ("All date and time information:%tc%n", date);//Format output date or time System.out.printf ("year-month-day format:%tf%n", date);    SYSTEM.OUT.PRINTF ("Month/day/year format:%td%n", date); System.out.printf ("HH:MM:SS PM format (12 o'clock):%tr%n ", date);    System.out.printf ("HH:MM:SS format (24 o'clock):%tt%n", date); System.out.printf ("hh:mm format (24 o'clock):%tr%n", date);} /*3, formatted date string B or H: The month abbreviation, as in: October English: Oct B: Full month, as in: October English: October A: The abbreviation of the week, such as: Saturday English: Sat A: Full name of the week, such as: Middle: Saturday |: Saturday C: The first two digits of the year (less than two Bit front 0), such as: 20y: The last two digits of the year (less than two bits before 0), such as: the Year of 07y:4 digits (less than 4 digits before 0), such as: 2007j: The number of days in a year (the day of the year), such as: two-digit month (less than two digits before 0), such as: 10d:                                              Two-digit day (less than two bits in front of 0), such as: 27e: The Day of the month (the front does not fill 0), such as: 5 */@Testpublic void Test3 () {date date=new date ();      Create Date Object String Str=string.format (locale.us, "English month Abbreviation:%TB", date);                                                                              Format Date string System.out.println (str);    Output string content System.out.printf ("Local month abbreviation:%tb%n", date);    Str=string.format (locale.us, "English month Full name:%TB", date);    System.out.println (str);    System.out.printf ("Local Month full name:%tb%n", date);    Str=string.format (locale.us, "English week Abbreviation:%ta", date);    System.out.println (str);    System.out.printf ("Local week abbreviation:%ta%n", date); System.out.printf("The first two digits of the year (less than two bits before 0):%tc%n", date);    System.out.printf ("the latter two digits of the year (less than two bits before 0):%ty%n", date);    System.out.printf ("Number of days in the Year" (The Day of the year):%tj%n ", date);    System.out.printf ("Two digits of the month (less than two bits before 0):%tm%n", date);    System.out.printf ("Two digits of the day (less than two bits in front of 0):%td%n", date); System.out.printf ("Day of the month (not before 0):%te", date);} /*4, Format time string H: 2-digit 24 o'clock hours (less than 2 bits in front of 0), such as: 15i:2-digit 12 o'clock hours (less than 2 bits before 0), such as: 03k:2-digit 24 o'clock hours (before 0), such as: 15l:2-digit 12 O'Clock hour ( Before 0), such as: 3m:2 digits of the minute (less than 2 bits in front of 0), such as: 03s:2 digit seconds (less than 2 bits in front of 0), such as: 09l:3 digit milliseconds (less than 3 bits in front of 0), such as: 015n:9 digits of the number of milliseconds (less than 9 bits before 0), such as: 562000000 P: Small Letter of the morning or afternoon mark, such as: Medium: p.m.: PM Z: Offset from GMT's RFC822 time zone, such as: +0800z: Time zone abbreviation string, such as: Csts:1970-1-1 00:00:00 to now the number of seconds elapsed,                               such as: 1193468128q:1970-1-1 00:00:00 to the current number of milliseconds elapsed, such as: 1193468128984 */@Testpublic void test4 () {date date=new date ();    Create Date Object System.out.printf ("2-digit 24 o'clock hour (less than 2 bits before 0):%th%n", date);    System.out.printf ("2-digit 12 o'clock hour (less than 2 bits in front 0):%ti%n", date);    System.out.printf ("2-digit 24 o'clock hour (front not 0):%tk%n", date); System.out.printf ("2-digit 12 o'clock hour (front not 0):%tl%n",Date);    System.out.printf ("2 digits in minutes (less than 2 bits before 0):%tm%n", date);    System.out.printf ("2 digit seconds (less than 2 bits in front of 0):%ts%n", date);    System.out.printf ("3 digits in milliseconds (less than 3 bits before 0):%tl%n", date);    System.out.printf ("9 digits in milliseconds (less than 9 bits in front of 0):%tn%n", date);    String Str=string.format (locale.us, "small letter morning or afternoon mark (English):%TP", date);                          System.out.println (str);    Output string Variable str contents System.out.printf ("lowercase letter of the morning or afternoon mark (medium):%tp%n", date);    System.out.printf ("Offset from RFC822 time zone relative to GMT:%tz%n", date);    System.out.printf ("time zone abbreviation string:%tz%n", date);    System.out.printf (number of seconds from 1970-1-1 00:00:00 to now:%ts%n, date);   SYSTEM.OUT.PRINTF (the number of milliseconds elapsed from 1970-1-1 00:00:00 to now:%tq%n, date); }}


String.Format () "Example in detail"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.