String.Format in Java

Source: Internet
Author: User
Tags locale rfc822

 Packagejunit.test;Importjava.util.Date;ImportJava.util.Locale;Importorg.junit.Test; Public classStringFormat {/*String.Format () Usage 1, conversion%s: String type, such as: "LJQ"%b: Boolean type, such as: true%d: integer type (decimal), such as: 99%f: Floating-point type, for example: 99.99%: Percent type, such as:%%n: newline character */@Test Public voidtest1 () {String str=NULL; STR=string.format ("Hi,%s", "Lin Yi-chin");//Formatting StringsSystem.out.println (str);//output string variable str contentSystem.out.printf ("3>7 The 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 date time format c: Include 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 System), such as: 14:28*/@Test Public voidtest2 () {Date Date=NewDate ();//Create Date ObjectSystem.out.printf ("All date and time information:%tc%n", date);//format output date or timeSYSTEM.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 System):%tt%n", date); System.out.printf ("hh:mm Format (24 o'clock System):%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 English: Saturday C: The first two digits of the year (not 0 on the front of the two digits), such as: 20y: The last two digits of the year (less than two bits before 0), such as: 07y:4 digit year (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 digits of the 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*/@Test Public voidtest3 () {Date Date=NewDate ();//Create Date ObjectString Str=string.format (locale.us, "English month Abbreviation:%TB", date);//Formatting Date StringsSystem.out.println (str);//Output String ContentsSystem.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 ("Number of years after two digits (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-digit month (less than two bits preceded by 0):%tm%n", date); System.out.printf ("Two-digit 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 in front of 0), such as: 03k:2-digit 24 o'clock hour (front does not fill 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*/@Test Public voidtest4 () {Date Date=NewDate ();//Create Date ObjectSystem.out.printf ("2-digit 24 o'clock hour (less than 2 bits in front 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 (not before 0):%tk%n", date); System.out.printf ("2-digit 12 o'clock hour (not before 0):%tl%n", date); System.out.printf ("2-digit minute (less than 2 bits preceded by 0):%tm%n", date); System.out.printf ("2 digits in seconds (less than 2 bits before 0):%ts%n", date); System.out.printf ("3-digit millisecond (less than 3 bits preceded by 0):%tl%n", date); System.out.printf ("Number of milliseconds for 9 digits (less than 9 bits before 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 contentSystem.out.printf ("Small Letter morning or afternoon mark (middle):%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 ("1970-1-1 00:00:00 to the present number of seconds elapsed:%ts%n", date); System.out.printf ("1970-1-1 00:00:00 to the present number of milliseconds:%tq%n", date);}}

String.Format in Java

Related Article

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.