Java format date, time, number, string __ formatted

Source: Internet
Author: User
Tags java format
first, format date and time


Example:

public void FormatDateTime () {
        dateformat format = new SimpleDateFormat ("YYYY year mm month DD Day  HH:mm:ss");
        Date date = new Date ();
        System.out.println (Format.format (date));
    }

Results: March 02, 2016 14:13:08 II, formatted numbers


Example:

public static void FormatNumber () {
    double d = 34563.88888;
    DecimalFormat format = new DecimalFormat ("##.###");
    System.out.println (Format.format (d));/will be rounded
    format.applypattern ("##.###%");
    System.out.println (Format.format (d));
    Format.applypattern ("###,###.000");
    System.out.println (Format.format (d));
}

Results:
34563.889
34,563,88.,888%
34,563.889 III. Format of general types


Example:

public static void FormatString () {String str=null;            
    Str=string.format ("hi,%s,%s,%s", "John", "Dick", "Harry");                           
    System.out.println (str);
    System.out.printf ("Capital of the letter A is:%c%n", ' a ');
    System.out.printf ("3>7 results are:%b%n", 3>7);
    System.out.printf ("Half of 100 is:%d%n", 100/2);  
    System.out.printf ("100 of the 16 number is:%x%n", 100);  
    System.out.printf ("100 of the 8 numbers are:%o%n", 100);  
    System.out.printf ("50 yuan book hit 8.5 discount is:%f Yuan%n", 50*0.85);  
    System.out.printf ("Above the price of 16 of the number is:%a%n", 50*0.85);  
    System.out.printf ("Above the price index says:%e%n", 50*0.85);  
    System.out.printf ("The index of the above price and the shorter length of the floating-point results are:%g%n", 50*0.85);
    System.out.printf ("The above discount is%d%%%n", 85); 
System.out.printf ("The hash code of the letter A is:%h%n", "a"); }

Results:
Hi, John, Dick, Harry
Letter a
3>7 The result is: false
100 of the half is: the 16 number of
100 is:
100 of the 8 number is: 144
50 Yuan's book 8.5 discount is: 42.500000 Yuan 16 of the above price is: 0X1.54P5
above the price index said: 4.250000e+01
The above price index and the length of the floating-point results is shorter: 42.5000
The above discount is 85%
The hash code of the letter A is: a

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.