Java string, date formatting

Source: Internet
Author: User

Common formatting in JSP includes:

1. String formatting

2. format the string and HTML display content

3. format the date

1. String formatting

String processing is the most frequently used in projects. Java provides two methods to format strings. JDBC also provides a question mark parameter similar to a formatting SQL statement.

1. format the string using the string. Format () method.

  Syntax format: public static string format (string format, object... ARGs)

  The placeholder (same as the C language) in the string is % d -- integer % f -- decimal % s -- string

   During formatting, replace the value of the variable in the placeholder order. If there are duplicate values, you need to write two % d Values and assign two values.

   For example:

       String
STR = "select top % d * From tb_user ";

       Str
= String. Format (STR, 10 );

2. format the string using messageformat.

    Class messageformat comes from the java. Text package

    Placeholders: {0}, {1}, or {1 [, format type [, format style]}

    The formats include number, date, time, and choice.

    For details about the format and style, see JDK help.

    For example:

    Int planet = 7;

    String event = "disturbance ";

   String result = messageformat. Format (

    "At {1, time} on {1, date}, there was {2} on planet {0, number, integer }.",

    Planet, new date (), event );

   Output:

       At
PM on Jul 3, 2053, there was disturbance on planet 7.

3. Question Mark parameters of SQL statements

   Use? Is a placeholder. The sequence starts from 1.

   Before creating a preparedstatement object to execute an SQL statement, set the parameter values for all question marks.

   Statement:

   Private void bindparameter (object... Params ){

        If (Params
! = NULL) {// variable parameters are used here. If no parameter exists, null is used. Otherwise, it is a parameter array and the type is object.

              Int
I = 1;// The parameter number starts from 1.

              For (Object
O: Params ){

                     If (o
= NULL ){

                         Pre. setnull (I, types. varchar );// Set the database field value to null

                     } Else
If (O instanceof java. util. Date) {// o if it is a Java. util. date object, true is returned.

                         Java. util. Date
Dd = (Java. util. Date) O;          

                         Java. SQL. Date
SD = new java. SQL. Date (DD. gettime ());

                         Pre. setdate (I,
SD );

                   } Else {

                         Pre. setobject (I,
O );

  

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.