"Go" Java formatted output printf example

Source: Internet
Author: User
Tags first string time and date

"Go" Java formatted output printf example

Transfer from http://www.cnblogs.com/TankMa/archive/2011/08/20/2146913.html#undefined

import java.util.Date;

/**

* Use printf Output

*/

/** Key Technology points

* Use Java.io.PrintStream's printf method for C-style output

* The first parameter of the printf method is the output format, the second parameter is variable length, indicating the data object to be output

*/

Public class Printf {

Public Static void Main (string[] args) {

/*** Output String ***/

%s represents the output string, which is the replacement of the following string in the schema%s

System. out. printf ("%s", new Integer (1212));

%n indicates line break

System. out. printf ("%s%n", "End Line");

Multiple parameters can also be supported

System. out. printf ("%s =%s%n", "Name", "Zhangsan");

%s exports the string in uppercase

System. out. printf ("%s =%s%n", "Name", "Zhangsan");

When multiple parameters are supported, the variable number can be inserted between%s, 1$ represents the first string, and 3$ represents the 3rd string

System. out. printf ("%1 $ s =%3$s%2$s%n", "Name", "san", "Zhang");

/*** Output Boolean type ***/

System. out. printf ("true =%b; False = ", true);

System. out. printf ("%b%n", false);

/*** Output Integer type ***/

Integer IOBJ = 342;

%d means to format integers as 10-binary integers

System. out. printf ("%d; %d %d%n ", -500, 2343L, IOBJ);

%o to format integers as 8-binary integers

System. out. printf ("%o;%o;%o %n ", -500, 2343L, IOBJ);

%x to format integers as 16 binary integers

System. out. printf ("%x; %x %x%n ", -500, 2343L, IOBJ);

%x to format an integer as a 16-in integer, and the letter becomes uppercase

System. out. printf ("%x; %x; %x%n ", -500, 2343L, IOBJ);

/*** output floating-point type ***/

Double dobj = 45.6d;

%e means to export floating-point numbers by scientific and technical law

System. out. printf ("%e; %e %e%n ", -756.403f, 7464.232641d, dobj);

%e represents the output of floating-point numbers in scientific and technical law, and in uppercase form

System. out. printf ("%E; %E; %e%n ", -756.403f, 7464.232641d, dobj);

%f indicates output floating-point numbers formatted in decimal

System. out. printf ("%f; %f %f%n ", -756.403f, 7464.232641d, dobj);

You can also limit the number of digits after the decimal point

System. out. printf ("%.1f; %.3f; %f%n ", -756.403f, 7464.232641d, dobj);

/*** Output Date Type ***/

%t represents a formatted datetime type,%t is an uppercase form of a time date, and a different output format is represented by a specific letter after%t

Date date = new date ();

long datal = Date.gettime ();

Format Month Day

%t followed by Y to indicate the year of the output date (2-digit year, such as 99)

%t followed by M for the month of the output date, followed by D to indicate the day number of the output date%t

System. out. printf ("%1$TY-%1$TM-%1$TD; %2$ty-%2$tm-%2$td%n ", date, datal);

%t followed by Y to indicate the year of the output date (4-digit year),

%t followed by B for the full name of the month of the output date, followed by a%t with B for the month of the output date

System. out. printf ("%1$TY-%1$TB-%1$TD; %2$ty-%2$tb-%2$td%n ", date, datal);

The following are common combinations of dates

%t followed by D to format the date with "%tm/%td/%ty"

System. out. printf ("%1$td%n", date);

%t followed by F to format the date with "%TY-%TM-%TD"

System. out. printf ("%1$tf%n", date);

/*** Output Time Type ***/

Output Time seconds

%t after the output time with H (24 binary), after%t with I for the time of output (12 binary),

%t is followed by M for the output time of the minute, after%t with s to indicate the output time of the second

System. out. printf ("%1$TH:%1$TM:%1$TS; %2$ti:%2$tm:%2$ts%n ", date, datal);

%t the millisecond in seconds after which the output time is expressed with L

System. out. printf ("%1$th:%1$tm:%1$ts%1$tl%n", date);

%t after p represents the morning or afternoon information of the output time

System. out. printf ("%1$th:%1$tm:%1$ts%1$tl%1$tp%n", date);

The following are common time combinations

After%t, use R to format time with "%th:%tm"

System. out. printf ("%1$tr%n", date);

%t after the "%th:%tm:%ts" format time with T

System. out. printf ("%1$tt%n", date);

After%t, use R to format time with "%ti:%tm:%ts%TP"

System. out. printf ("%1$tr%n", date);

/*** Output Week ***/

After%t, use a to get the full name of the day of the week.

System. out. printf ("%1$tf%1$ta%n", date);

%t to get the abbreviation for the day of the week with a

System. out. printf ("%1$tf%1$ta%n", date);

Complete information for the output time and date

System. out. printf ("%1$tc%n", date);

}

}

/**

In the *printf method, the format "%s" means that the first parameter value of the second variable-length parameter is output as a string;

* format "%n" for newline; format '%s ' means to export the string in uppercase; "n$" between "%s"

* Outputs the nth parameter value of a variable-length parameter. Format "%b" to indicate output of the second variable length parameter as a Boolean value

* The first parameter value.

*/

/**

* format "%d" means output in decimal integer; "%o" The expression is output in eight binary form; %x "indicates a hexadecimal

* OUTPUT; " %x "indicates the output in hexadecimal, and the letters (A, B, C, D, E, F) are capitalized. The format is"%e "table

* Output floating-point numbers in scientific notation; the format "%E" means to output floating-point numbers in scientific notation, and to capitalize E in the format

* "%f" means output in decimal floating-point numbers, and ". N" Between "%f" indicates that the output retains the n digits after the decimal point.

*/

/**

* format "%t" indicates the output time date type. " %t "followed by Y for the two-digit year of the output date (for example, 99), with M

* Indicates the month of the output date, using D to indicate the day of the output date; " %t "followed by Y represents the four-digit year of the output date

* (such as 1999), the full name of the month in which the output date is represented by B, and b for the month of the output date. " %t "followed by D

* Indicates that the date is output in the format "%tm/%td/%ty", and the date in "%TY-%TM-%TD" is expressed in F.

*/

/**

* "%t" after "H" for the time of output (24), with I for the time of output (12), with M for the output time

* points, with S for the output time of the second, with L for the output time of the second of the number of milliseconds, with p for the output time is the morning or

* Afternoon. " %t "then use R to indicate the output time in"%TH:%TM "format, with T for"%th:%tm:%ts "format output

* time, using R to indicate the output time in "%ti:%tm:%ts%TP" format.

*/

/**

* "%t" after "a" to indicate the full name of the output date, a for the output date of the week abbreviation.

*/

"Go" Java formatted output printf example

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.