`

java.text--format

阅读更多
   总体的来说formate主要涉及到三个方法。即为format(),parse(),及中间配合Local的使用
   1.NumberFormat常用格式化语句:   
     1.1 DecimalFormat的基本使用
        NumberFormat format = new DecimalFormat("000000");;
        String s = format.format(-1234.567);    //-001235
        
        NumberFormat format1 = new DecimalFormat("##");
        String ss = format1.format(-1234.567);  //-001235
        
        
        NumberFormat format2 = new DecimalFormat(".00");
        String sString = format2.format(0.567);     //.57
        
        NumberFormat format3 = new DecimalFormat("0.00");
        String string2 = format3.format(0.567);     //0.57
        
        NumberFormat format4 = new DecimalFormat("#.#");
        String s1 = format4.format(1234.567);   //1234.6
        
        NumberFormat format5 = new DecimalFormat("#.000000");
        String s3 = format5.format(1234.567);   //1234.567000
        
        NumberFormat format6 = new DecimalFormat("#,###,###");
        String s4 = format6.format(1234.567);   //1235
        
        format6 = new DecimalFormat("'$'#");
        String s5 = format6.format(-1234.567);  //-$1235
        
        format6 = new DecimalFormat("'abc'#");
        s5 = format6.format(-1234.567);  //-abc1235
      

    //不同国家地区对于数值的格式化方式是不一样的
        Locale locale = Locale.CANADA;
        String s = NumberFormat.getInstance(locale).format(-1234.56);//-1,234.56
        locale = Locale.GERMAN;
        s = NumberFormat.getInstance(locale).format(-1234.56);       //-1.234,56
        s = NumberFormat.getInstance().format(-1234.56);             //-1,234.56
        
        try
        {
            Number number = NumberFormat.getInstance(Locale.GERMAN).parse("-1.234,56");
            if(number instanceof Long){
                System.out.println("it's Long type");
            }else if(number instanceof Double){
                System.out.println("it's Double type");              //在这行输出结果
            }
        }
        catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    1.2取得货币的格式化
        Locale locale = Locale.CANADA;
        String string = NumberFormat.getCurrencyInstance(locale).format(123.45);//$123.45   这里是货币的格式化方式

    1.3取得百分比的格式化 
        Locale locale1 = Locale.CANADA;
        String pp = NumberFormat.getPercentInstance(locale1).format(123.456);   //这里是百分比的格式化形式
      
    
  2.DateFormat的基本使用

      2.1基本使用 
         DateFormat dFormat= new SimpleDateFormat("HH:mm:ss a");   //a 会根据不同的地区或国家来显示不同的时段,在中国就是上午的意思
         String pp = dFormat.format(new Date());        //08.59.28 上午
      若是我们在     DateFormat dFormat= new SimpleDateFormat("HH:mm:ss a",Locale.ENGLISH);那么出现的就是
         String pp = dFormat.format(new Date());        //08.59.28 AM
      也就是说会根据不同的地区来显示。
        
      df = new SimpleDateFormat("HH:mm:ss Z");可以加上Local.ENGLISH
      Date date2 = (Date)df.parse("22:14:02 -0500");//Fri Jan 02 11:14:02 CST 1970 Z代表时区的概念
 
     2.2取得在一周是星期几
       DateFormat dFormat = new SimpleDateFormat("EEEE",Locale.CHINA);//若是在默认的情况下就是取得默认地区的形式
      System.out.println(dFormat.format(new Date()));     //星期三

    2.3 SimpleDateFormat的基本使用
        若是 
java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);// 表示24小时制
              java.text.SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss",Locale.CHINA);//表示12小时制

2.4 常用转化以 将一个String类型的数值转化为DATE类型。
            DateFormat df = new SimpleDateFormat("yyyyMMddHHhhss");        ————这里最关键
            Date pp = df.parse("20081002102030");
            DateFormat p1p = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(p1p.format(pp));

           // 如此即可以把一个String类型的转化为自己需要形式的String类型  

  3.MessageFormat的使用
     Object[] params = new Object[]{new Integer(123), new Integer(1234)};
    String msg = MessageFormat.format("There are {0} a''s and {1} b''s", params);
    // There are 123 a's and 1,234 b's
    
    msg = MessageFormat.format("There are {0,number} a''s and {1,number} b''s", params);
    // There are 123 a's and 1,234 b's
    
    // Use a custom format; see e311 Formatting a Number Using a Custom Format
    msg = MessageFormat.format("There are {0,number,#} a''s and {1,number,#} b''s", params);
    // There are 123 a's and 1234 b's
    
    // Floating point numbers
    params = new Object[]{new Double(123.45), new Double(1234.56)};
    msg = MessageFormat.format("There are {0,number,#.#} a''s and {1,number,#.#} b''s", params);
    // There are 123.4 a's and 1234.6 b's
    
    // Currency
    msg = MessageFormat.format("There are {0,number,currency} a''s and {1,number,currency} b''s", params);
    // There are $123.00 a's and $1,234.00 b's
    
    // Percent
    msg = MessageFormat.format("There are {0,number,percent} a''s and {1,number,percent} b''s", params);
    // There are 12,345% a's and 123,456% b's
 

  
     
分享到:
评论

相关推荐

    vscode插件合集(20190814)part1

    vscode最新插件合集(20190814)part1,共2个分包(part1...foxundermoon.shell-format-6.1.1 qcz.text-power-tools-1.11.1 gruntfuggly.todo-tree-0.0.141 vscode-icons-team.vscode-icons-9.2.0 tomoki1207.pdf-0.5.0

    java.text.ParseException: Unparseable date: 2/10/2010 15:20:05

    NULL 博文链接:https://speed-guo.iteye.com/blog/903163

    vscode插件合集(20190814)part2

    vscode最新插件合集(20190814)part1,共2个分包(part1...foxundermoon.shell-format-6.1.1 qcz.text-power-tools-1.11.1 gruntfuggly.todo-tree-0.0.141 vscode-icons-team.vscode-icons-9.2.0 tomoki1207.pdf-0.5.0

    FastReport Professional v3.0 Full Source

    - Storing reports in XML format. - Precise object's coordinates and sizes. - Full WYSIWYG (now for text objects too). - New object - diagonal line. - New fill types, shadow. - Text rotation 0.....

    php-java-bridge 配置包

    print $formatter ->format( new Java( 'java.util.Date' )); ?> 6.启动apache,在浏览器中查看 http://localhost/demo/test.php 会看到如下信息: php-java-bridge config... Java version=1.6.0_10 Java ...

    ORACLE OSB开发指南

    Creating Message Format Files ........................................................................................................ 2-5 Exporting Resources ............................................

    智能识别收货地址Java.pdf

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.TimeZone; import javax.crypto.Mac; import javax...

    欧柏泰克:Java常用类库--日期操作类

    DateFormat 类是 Java 语言中用于格式化日期和时间的类,位于 java.text 包中。DateFormat 类提供了对日期和时间的格式化操作,如将日期和时间格式化为字符串。 例如: ```java import java.text.DateFormat; ...

    JAVA_API1.6文档(中文)

    javax.swing.text.rtf 提供一个类 (RTFEditorKit),用于创建富文本格式(Rich-Text-Format)的文本编辑器。 javax.swing.tree 提供处理 javax.swing.JTree 的类和接口。 javax.swing.undo 允许开发人员为应用程序...

    JavaAPI中文chm文档 part2

    javax.swing.text.rtf 提供一个类 (RTFEditorKit),用于创建富文本格式(Rich-Text-Format)的文本编辑器。 javax.swing.tree 提供处理 javax.swing.JTree 的类和接口。 javax.swing.undo 允许开发人员为应用程序...

    Java超市会员管理系统(时间类)

    import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[] args) { //当前日期和时间 Date date=new Date(); System.out.println(date); //将...

    java pdf 查看器

    /** the current page number text field */ JTextField pageField; /** the full screen window, or null if not in full screen mode */ FullScreenWindow fullScreen; /** the root of the outline, or null...

    Java 1.6 API 中文 New

    javax.swing.text.rtf 提供一个类 (RTFEditorKit),用于创建富文本格式(Rich-Text-Format)的文本编辑器。 javax.swing.tree 提供处理 javax.swing.JTree 的类和接口。 javax.swing.undo 允许开发人员为应用程序...

    java api最新7.0

    javax.swing.text.rtf 提供一个类 (RTFEditorKit),用于创建富文本格式(Rich-Text-Format)的文本编辑器。 javax.swing.tree 提供处理 javax.swing.JTree 的类和接口。 javax.swing.undo 允许开发人员为应用程序...

    JavaAPI1.6中文chm文档 part1

    javax.swing.text.rtf 提供一个类 (RTFEditorKit),用于创建富文本格式(Rich-Text-Format)的文本编辑器。 javax.swing.tree 提供处理 javax.swing.JTree 的类和接口。 javax.swing.undo 允许开发人员为应用程序...

    FastReport 3.0 Trial for Delphi5 无源码

    Storing reports in XML format. Precise object‘s coordinates and sizes. Full WYSIWYG (now for text objects too). New object - diagonal line. New fill types, shadow. Text rotation 0..360 degrees. ...

    FastReport 3.0 Trial for Delphi6 无源码

    Storing reports in XML format. Precise object‘s coordinates and sizes. Full WYSIWYG (now for text objects too). New object - diagonal line. New fill types, shadow. Text rotation 0..360 degrees. ...

    FastReport 3.0 Trial for Delphi7 无源码

    Storing reports in XML format. Precise object‘s coordinates and sizes. Full WYSIWYG (now for text objects too). New object - diagonal line. New fill types, shadow. Text rotation 0..360 degrees. ...

    UE(官方下载)

    The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...

Global site tag (gtag.js) - Google Analytics