欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java 中 String format 和Math類實(shí)例詳解

 更新時(shí)間:2017年06月19日 14:27:42   投稿:lqh  
這篇文章主要介紹了java 中 String format 和Math類實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下

java 中 String format 和Math類實(shí)例詳解

java字符串格式化輸出

@Test 
public void test() { 
  // TODO Auto-generated method stub 
  //可用printf(); 
  System.out.println(String.format("I am %s", "jj"));     //%s字符串 
  System.out.println(String.format("首字母是 %c", 'x'));     //%c字符 
  System.out.println(String.format("this is %b", true));   //%b布爾類型 
  System.out.println(String.format("十進(jìn)制整數(shù) %d", 34));     //%d 十進(jìn)制整數(shù) 
  System.out.println(String.format("十六進(jìn)制整數(shù) %x", 34));   //%x 十六進(jìn)制整數(shù) 
  System.out.println(String.format("八進(jìn)制整數(shù) %o", 34));     //%o 八進(jìn)制整數(shù) 
  System.out.println(String.format("浮點(diǎn) %f", 34.0));      //%f 浮點(diǎn) 
  System.out.println(String.format("十六進(jìn)制浮點(diǎn) %a", 34.0));    //%a 十六進(jìn)制浮點(diǎn) 
  System.out.println(String.format("指數(shù) %e", 34.0));      //%e 指數(shù)類型 
  System.out.println(String.format("通用浮點(diǎn)類型 %g", 34.0));    //%g 通用浮點(diǎn) 
  System.out.println(String.format("散列碼 %h", 34));      //%h 散列碼 
  System.out.println(String.format("百分比 %%"));        //%% 百分比 
  System.out.println(String.format("換行 %n"));         //%n 換行 
  System.out.println(String.format("日期與事件類型 %ty",Calendar.getInstance())); 
  System.out.println(String.format("日期與事件類型 %tm",Calendar.getInstance())); 
  System.out.println(String.format("日期與事件類型 %te",Calendar.getInstance())); 
  //%tx 日期與事件類型,x代表不同的日期與時(shí)間轉(zhuǎn)換符 %ty 年 %tm月 %te 日 
  //搭配轉(zhuǎn)換符的使用 
  System.out.println(String.format("%+d", 10));        //為正數(shù)或負(fù)數(shù)添加符號(hào) 
  System.out.println(String.format("|%-5d|", 10));      //%-?為左對(duì)齊 
  System.out.println(String.format("%04d", 10));       //在整數(shù)之前添加指定數(shù)量空格 
  System.out.println(String.format("%,f", 999999999.0));   //以“,”對(duì)數(shù)字分組 
  System.out.println(String.format("%(f", -999999999.0));   //使用括號(hào)包含負(fù)數(shù) 
  System.out.println(String.format("%#x", 34));        //十六進(jìn)制添加0x 
  System.out.println(String.format("%#o", 34));        //八進(jìn)制添加0 
  System.out.println(String.format("%#f", 34.0));       //浮點(diǎn)數(shù)包含小數(shù)點(diǎn) 
  System.out.println(String.format("%f 和%<3.1f", 34.0f));   //格式化前一個(gè)轉(zhuǎn)換符所描述的參數(shù)(小數(shù)后有一位) 
  System.out.println(String.format("%3.1f", 34.0f));   // 
  System.out.println(String.format("%2$d,%1$s", "a",1));   // x$代表是第幾個(gè)變量 
  //日期格式化 
  System.out.println(String.format("全部日期和時(shí)間信息%tc", new Date()));   // tc 輸出全部日期和時(shí)間信息 
  System.out.println(String.format("年—月—日格式%tF", new Date()));      // tF 年—月—日格式(要大寫(xiě)) 
  System.out.println(String.format("月/日/年格式%tD", new Date()));      // tD 月/日/年格式(要大寫(xiě)) 
  System.out.println(String.format("HH:MM:SS PM/AM格式 %tr", new Date())); // tR HH:MM:SS PM/AM格式 
  System.out.println(String.format("HH:MM:SS(24小時(shí))%tT", new Date())); // (大寫(xiě))tT HH:MM:SS 24小時(shí)制 
  System.out.println(String.format("HH:MM(24小時(shí))%tR", new Date()));  // (大寫(xiě))tR HH:MM 24小時(shí)制 
   
  System.out.println(String.format(Locale.US,"英文月份簡(jiǎn)稱%tb", new Date()));    // tb 輸出月份簡(jiǎn)稱 
  System.out.println(String.format("本地月份簡(jiǎn)稱%tb", new Date()));       // tb 輸出月份簡(jiǎn)稱 
  System.out.println(String.format(Locale.US,"英文月份全稱%tB", new Date()));    // tB 輸出月份全稱 
  System.out.println(String.format("本地月份全稱%tB", new Date()));       // tB 輸出月份全稱 
  System.out.println(String.format(Locale.US,"星期簡(jiǎn)稱%ta", new Date()));   // ta 輸出星期簡(jiǎn)稱 
  System.out.println(String.format("星期全稱%tA", new Date()));        // tA 輸出星期全稱 
  Date date = new Date(); 
  System.out.printf("本地星期的簡(jiǎn)稱:%tA%n",date); 
  //C的使用,年前兩位 
  System.out.printf("年的前兩位數(shù)字(不足兩位前面補(bǔ)0):%tC%n",date); 
  //y的使用,年后兩位 
  System.out.printf("年的后兩位數(shù)字(不足兩位前面補(bǔ)0):%ty%n",date); 
  //j的使用,一年的天數(shù) 
  System.out.printf("一年中的天數(shù)(即年的第幾天):%tj%n",date); 
  //m的使用,月份 
  System.out.printf("兩位數(shù)字的月份(不足兩位前面補(bǔ)0):%tm%n",date); 
  //d的使用,日(二位,不夠補(bǔ)零) 
  System.out.printf("兩位數(shù)字的日(不足兩位前面補(bǔ)0):%td%n",date); 
  //e的使用,日(一位不補(bǔ)零) 
  System.out.printf("月份的日(前面不補(bǔ)0):%te",date); 
 //H的使用 
    System.out.printf("2位數(shù)字24時(shí)制的小時(shí)(不足2位前面補(bǔ)0):%tH%n", date); 
    //I的使用 
    System.out.printf("2位數(shù)字12時(shí)制的小時(shí)(不足2位前面補(bǔ)0):%tI%n", date); 
    //k的使用 
    System.out.printf("2位數(shù)字24時(shí)制的小時(shí)(前面不補(bǔ)0):%tk%n", date); 
    //l的使用 
    System.out.printf("2位數(shù)字12時(shí)制的小時(shí)(前面不補(bǔ)0):%tl%n", date); 
    //M的使用 
    System.out.printf("2位數(shù)字的分鐘(不足2位前面補(bǔ)0):%tM%n", date); 
    //S的使用 
    System.out.printf("2位數(shù)字的秒(不足2位前面補(bǔ)0):%tS%n", date); 
    //L的使用 
    System.out.printf("3位數(shù)字的毫秒(不足3位前面補(bǔ)0):%tL%n", date); 
    //N的使用 
    System.out.printf("9位數(shù)字的毫秒數(shù)(不足9位前面補(bǔ)0):%tN%n", date); 
    //p的使用 
    String str = String.format(Locale.US, "小寫(xiě)字母的上午或下午標(biāo)記(英):%tp", date); 
    System.out.println(str);  
    System.out.printf("小寫(xiě)字母的上午或下午標(biāo)記(中):%tp%n", date); 
    //z的使用 
    System.out.printf("相對(duì)于GMT的RFC822時(shí)區(qū)的偏移量:%tz%n", date); 
    //Z的使用 
    System.out.printf("時(shí)區(qū)縮寫(xiě)字符串:%tZ%n", date); 
    //s的使用 
    System.out.printf("1970-1-1 00:00:00 到現(xiàn)在所經(jīng)過(guò)的秒數(shù):%ts%n", date); 
    //Q的使用 
    System.out.printf("1970-1-1 00:00:00 到現(xiàn)在所經(jīng)過(guò)的毫秒數(shù):%tQ%n", date); 
} 

Math

@Test 
  public void test3(){ 
    BigDecimal d = new BigDecimal("123"); 
    BigDecimal e = new BigDecimal("14455552"); 
    System.out.println(Math.pow(123, 12)); 
    System.out.println(d.pow(12)); 
     
    System.out.println(Math.ceil(12.3));//ceil,天花板 13.0 
    System.out.println(Math.floor(-12.3));//ceil,地板 -13.0 
    System.out.println(Math.round(13.3));//四舍五入  13 
    System.out.println(Math.round(-13.5));//四舍五入   -13 
    System.out.println(Math.round(-13.2));//四舍五入   -13 
    System.out.println(Math.round(-13.7));//四舍五入   -14 
  } 

隨機(jī)數(shù)

//隨機(jī)數(shù) 
  @Test 
  public void test4(){ 
    System.out.println(Math.random());//返回一個(gè)隨機(jī)數(shù)>=0,+b1 
    //0-100(不包括100) 
    int a =(int)(Math.random()*100); 
    //0-100(包括100) 
    int b =(int)(Math.random()*101); 
    //30-100(包括100) 
    int c =(int)(Math.random()*71+30); 
    //0-10 
    int d = (int)(Math.random()*10); 
    System.out.println(a); 
    System.out.println(b); 
    System.out.println(c); 
    System.out.println(d); 
  } 
@Test 
public void test5(){ 
  Random r = new Random(); 
  int a = r.nextInt(101);//0-100 
   
} 

simpledateformat

@Test 
  public void testId() throws ParseException{ 
    String s = "411123199409203013"; 
    if(s.length()==18){ 
      String b = s.substring(6, 14); 
      String sex = Integer.parseInt(s.substring(14, 17))%2==0?"女":"男"; 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
      Date birth = sdf.parse(b);//將一個(gè)日期字符串按指定的格式解析成日期對(duì)象 
      sdf = new SimpleDateFormat("yyyy年MM月dd日"); 
      System.out.println("你的生日是"+sdf.format(birth)+",你的性別是"+sex); 
      String f = String.format("你的生日是%1$TY年%1$Tm月%1$Td日,你的性別是%2$s", birth,sex); 
      System.out.println(f); 
    }else if(s.length()==15){ 
      String b = s.substring(6, 14); 
      String sex = Integer.parseInt(s.substring(14, 17))%2==0?"女":"男"; 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); 
      Date birth = sdf.parse(b);//將一個(gè)日期字符串按指定的格式解析成日期對(duì)象 
      sdf = new SimpleDateFormat("yyyy年MM月dd日"); 
      System.out.println("你的生日是"+sdf.format(birth)+",你的性別是"+sex);//format將日期對(duì)象生成指定格式的字符串 
       
       
    } 

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Java跨域問(wèn)題的處理詳解

    Java跨域問(wèn)題的處理詳解

    這篇文章主要給大家介紹了關(guān)于Java跨域問(wèn)題處理的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-03-03
  • 淺析Java中的Caffeine緩存源碼

    淺析Java中的Caffeine緩存源碼

    這篇文章主要介紹了淺析Java中的Caffeine緩存源碼,Caffeine是一個(gè)Java開(kāi)發(fā)的高性能緩存庫(kù),它提供了一種簡(jiǎn)單而強(qiáng)大的方式來(lái)管理內(nèi)存中的緩存數(shù)據(jù),Caffeine的設(shè)計(jì)目標(biāo)是提供快速、高效的緩存訪問(wèn),同時(shí)保持簡(jiǎn)單易用的API,本文針對(duì)其部分源碼做出解析,需要的朋友可以參考下
    2023-10-10
  • 詳解微信開(kāi)發(fā)之Author網(wǎng)頁(yè)授權(quán)

    詳解微信開(kāi)發(fā)之Author網(wǎng)頁(yè)授權(quán)

    微信開(kāi)發(fā)中,經(jīng)常有這樣的需求:獲得用戶頭像、綁定微信號(hào)給用戶發(fā)信息,那么實(shí)現(xiàn)這些的前提就是授權(quán)!本文對(duì)此進(jìn)行系統(tǒng)介紹,需要的朋友一起來(lái)看下吧
    2016-12-12
  • Java實(shí)現(xiàn)在線SQL編程最新完整版

    Java實(shí)現(xiàn)在線SQL編程最新完整版

    這篇文章主要介紹了Java實(shí)現(xiàn)在線SQL編程,在使用JDBC來(lái)進(jìn)行數(shù)據(jù)庫(kù)的操作,在使用時(shí)由于對(duì)其方法并不是全部了解,所以需要邊看源碼邊改善功能,感興趣的朋友跟隨小編一起看看吧
    2022-06-06
  • java字符串常用操作方法(查找、截取、分割)

    java字符串常用操作方法(查找、截取、分割)

    今天小編就為大家分享一篇java字符串常用操作方法(查找、截取、分割),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Java編程中最基礎(chǔ)的文件和目錄操作方法詳解

    Java編程中最基礎(chǔ)的文件和目錄操作方法詳解

    這篇文章主要介紹了Java編程中最基礎(chǔ)的文件和目錄操作方法詳解,是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-11-11
  • Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法

    Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法

    這篇文章主要介紹了Windows系統(tǒng)中Java調(diào)用cmd命令及執(zhí)行exe程序的方法,主要用到了IOException類,需要的朋友可以參考下
    2016-03-03
  • SpringBoot集成XXL-JOB實(shí)現(xiàn)靈活控制的分片處理方案

    SpringBoot集成XXL-JOB實(shí)現(xiàn)靈活控制的分片處理方案

    因?yàn)樾枰⑿刑幚硗粡垟?shù)據(jù)表里的數(shù)據(jù),所以比較自然地想到了分片查詢數(shù)據(jù),可以利用對(duì) id 取模的方法進(jìn)行分片,避免同一條數(shù)據(jù)被重復(fù)處理,所以本文給大家介紹了SpringBoot集成XXL-JOB實(shí)現(xiàn)靈活控制的分片處理方案,需要的朋友可以參考下
    2024-09-09
  • Spring注解方式無(wú)法掃描Service注解的解決

    Spring注解方式無(wú)法掃描Service注解的解決

    這篇文章主要介紹了Spring注解方式無(wú)法掃描Service注解的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Java中MapStruct入門(mén)使用及對(duì)比

    Java中MapStruct入門(mén)使用及對(duì)比

    MapStruct是一個(gè)Java注解處理器框架,用于簡(jiǎn)化Java Bean之間的映射,本文主要介紹了Java中MapStruct入門(mén)使用及對(duì)比,感興趣的可以了解一下
    2023-12-12

最新評(píng)論