最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java 中 String format 和Math類實例詳解

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

java 中 String format 和Math類實例詳解

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("十進制整數(shù) %d", 34));     //%d 十進制整數(shù) 
  System.out.println(String.format("十六進制整數(shù) %x", 34));   //%x 十六進制整數(shù) 
  System.out.println(String.format("八進制整數(shù) %o", 34));     //%o 八進制整數(shù) 
  System.out.println(String.format("浮點 %f", 34.0));      //%f 浮點 
  System.out.println(String.format("十六進制浮點 %a", 34.0));    //%a 十六進制浮點 
  System.out.println(String.format("指數(shù) %e", 34.0));      //%e 指數(shù)類型 
  System.out.println(String.format("通用浮點類型 %g", 34.0));    //%g 通用浮點 
  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代表不同的日期與時間轉(zhuǎn)換符 %ty 年 %tm月 %te 日 
  //搭配轉(zhuǎn)換符的使用 
  System.out.println(String.format("%+d", 10));        //為正數(shù)或負數(shù)添加符號 
  System.out.println(String.format("|%-5d|", 10));      //%-?為左對齊 
  System.out.println(String.format("%04d", 10));       //在整數(shù)之前添加指定數(shù)量空格 
  System.out.println(String.format("%,f", 999999999.0));   //以“,”對數(shù)字分組 
  System.out.println(String.format("%(f", -999999999.0));   //使用括號包含負數(shù) 
  System.out.println(String.format("%#x", 34));        //十六進制添加0x 
  System.out.println(String.format("%#o", 34));        //八進制添加0 
  System.out.println(String.format("%#f", 34.0));       //浮點數(shù)包含小數(shù)點 
  System.out.println(String.format("%f 和%<3.1f", 34.0f));   //格式化前一個轉(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$代表是第幾個變量 
  //日期格式化 
  System.out.println(String.format("全部日期和時間信息%tc", new Date()));   // tc 輸出全部日期和時間信息 
  System.out.println(String.format("年—月—日格式%tF", new Date()));      // tF 年—月—日格式(要大寫) 
  System.out.println(String.format("月/日/年格式%tD", new Date()));      // tD 月/日/年格式(要大寫) 
  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小時)%tT", new Date())); // (大寫)tT HH:MM:SS 24小時制 
  System.out.println(String.format("HH:MM(24小時)%tR", new Date()));  // (大寫)tR HH:MM 24小時制 
   
  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,"英文月份全稱%tB", new Date()));    // tB 輸出月份全稱 
  System.out.println(String.format("本地月份全稱%tB", new Date()));       // tB 輸出月份全稱 
  System.out.println(String.format(Locale.US,"星期簡稱%ta", new Date()));   // ta 輸出星期簡稱 
  System.out.println(String.format("星期全稱%tA", new Date()));        // tA 輸出星期全稱 
  Date date = new Date(); 
  System.out.printf("本地星期的簡稱:%tA%n",date); 
  //C的使用,年前兩位 
  System.out.printf("年的前兩位數(shù)字(不足兩位前面補0):%tC%n",date); 
  //y的使用,年后兩位 
  System.out.printf("年的后兩位數(shù)字(不足兩位前面補0):%ty%n",date); 
  //j的使用,一年的天數(shù) 
  System.out.printf("一年中的天數(shù)(即年的第幾天):%tj%n",date); 
  //m的使用,月份 
  System.out.printf("兩位數(shù)字的月份(不足兩位前面補0):%tm%n",date); 
  //d的使用,日(二位,不夠補零) 
  System.out.printf("兩位數(shù)字的日(不足兩位前面補0):%td%n",date); 
  //e的使用,日(一位不補零) 
  System.out.printf("月份的日(前面不補0):%te",date); 
 //H的使用 
    System.out.printf("2位數(shù)字24時制的小時(不足2位前面補0):%tH%n", date); 
    //I的使用 
    System.out.printf("2位數(shù)字12時制的小時(不足2位前面補0):%tI%n", date); 
    //k的使用 
    System.out.printf("2位數(shù)字24時制的小時(前面不補0):%tk%n", date); 
    //l的使用 
    System.out.printf("2位數(shù)字12時制的小時(前面不補0):%tl%n", date); 
    //M的使用 
    System.out.printf("2位數(shù)字的分鐘(不足2位前面補0):%tM%n", date); 
    //S的使用 
    System.out.printf("2位數(shù)字的秒(不足2位前面補0):%tS%n", date); 
    //L的使用 
    System.out.printf("3位數(shù)字的毫秒(不足3位前面補0):%tL%n", date); 
    //N的使用 
    System.out.printf("9位數(shù)字的毫秒數(shù)(不足9位前面補0):%tN%n", date); 
    //p的使用 
    String str = String.format(Locale.US, "小寫字母的上午或下午標(biāo)記(英):%tp", date); 
    System.out.println(str);  
    System.out.printf("小寫字母的上午或下午標(biāo)記(中):%tp%n", date); 
    //z的使用 
    System.out.printf("相對于GMT的RFC822時區(qū)的偏移量:%tz%n", date); 
    //Z的使用 
    System.out.printf("時區(qū)縮寫字符串:%tZ%n", date); 
    //s的使用 
    System.out.printf("1970-1-1 00:00:00 到現(xiàn)在所經(jīng)過的秒數(shù):%ts%n", date); 
    //Q的使用 
    System.out.printf("1970-1-1 00:00:00 到現(xiàn)在所經(jīng)過的毫秒數(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 
  } 

隨機數(shù)

//隨機數(shù) 
  @Test 
  public void test4(){ 
    System.out.println(Math.random());//返回一個隨機數(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);//將一個日期字符串按指定的格式解析成日期對象 
      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);//將一個日期字符串按指定的格式解析成日期對象 
      sdf = new SimpleDateFormat("yyyy年MM月dd日"); 
      System.out.println("你的生日是"+sdf.format(birth)+",你的性別是"+sex);//format將日期對象生成指定格式的字符串 
       
       
    } 

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

相關(guān)文章

  • JVM Tomcat性能實戰(zhàn)(推薦)

    JVM Tomcat性能實戰(zhàn)(推薦)

    下面小編就為大家?guī)硪黄狫VM Tomcat性能實戰(zhàn)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • Java 數(shù)據(jù)結(jié)構(gòu)之刪除鏈表中重復(fù)的結(jié)點

    Java 數(shù)據(jù)結(jié)構(gòu)之刪除鏈表中重復(fù)的結(jié)點

    在一個排序的鏈表中,會存在重復(fù)的結(jié)點,如何實現(xiàn)刪除該鏈表中重復(fù)的結(jié)點,重復(fù)的結(jié)點不保留,并返回鏈表頭指針呢?接下來小編將帶你詳細介紹
    2021-12-12
  • Spring Cloud Config工作原理概述

    Spring Cloud Config工作原理概述

    Spring Cloud Config 是 Spring Cloud 生態(tài)系統(tǒng)的一部分,它提供了一種集中化管理應(yīng)用配置的方法,本文給大家介紹Spring Cloud Config工作原理概述,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • Java實現(xiàn)JS中的escape和UNescape代碼分享

    Java實現(xiàn)JS中的escape和UNescape代碼分享

    在PHP和Python中都有類似JS中的escape和UNescape函數(shù)的功能,那么Java語言中到底有沒有類似的方法呢?本文就來介紹一下Java實現(xiàn)JS中的escape和UNescape轉(zhuǎn)碼方法,需要的朋友可以參考下
    2017-09-09
  • Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解

    Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解

    這篇文章主要介紹了Java SpringBoot在RequestBody中高效的使用枚舉參數(shù)原理案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 淺談spring aop的五種通知類型

    淺談spring aop的五種通知類型

    這篇文章主要介紹了淺談spring aop的五種通知類型,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • JAVA查詢MongoDB的幾種方法小結(jié)

    JAVA查詢MongoDB的幾種方法小結(jié)

    本文主要介紹了JAVA查詢MongoDB的幾種方法小結(jié),通過閱讀本文,讀者可以了解如何使用Java查詢MongoDB,并在實際應(yīng)用中應(yīng)用這些技能,具有一定的參考價值,感興趣的可以了解一下
    2023-08-08
  • IDEA自動生成類圖和時序圖的操作指南

    IDEA自動生成類圖和時序圖的操作指南

    idea 的強大之處在于此,它包含了很多小插件,我們不需要再次下載相關(guān)插件,只需要在idea中小小的設(shè)置一下就可以了,本文我介紹了IDEA自動生成類圖和時序圖的操作指南,我用的是idea2020版本,需要的朋友可以參考下
    2024-05-05
  • SpringMVC如何把后臺文件打印到前臺

    SpringMVC如何把后臺文件打印到前臺

    這篇文章主要介紹了SpringMVC如何把后臺文件打印到前臺,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • MyBatis如何調(diào)用存儲過程與存儲函數(shù)

    MyBatis如何調(diào)用存儲過程與存儲函數(shù)

    這篇文章主要介紹了MyBatis如何調(diào)用存儲過程與存儲函數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11

最新評論

车险| 昌邑市| 衢州市| 讷河市| 全州县| 奉新县| 湘阴县| 甘洛县| 云阳县| 南江县| 资中县| 辰溪县| 东兰县| 浏阳市| 自治县| 廉江市| 原阳县| 枣阳市| 连江县| 宁城县| 甘德县| 井研县| 鸡泽县| 南郑县| 万源市| 东海县| 巴里| 疏勒县| 武威市| 涪陵区| 芦溪县| 平泉县| 津市市| 疏附县| 朝阳市| 沾益县| 五常市| 舟曲县| 五河县| 洛川县| 吉林省|