zoukankan      html  css  js  c++  java
  • String.format()方法 “%1$01d” "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"日期转换等记录

        /**
    * %后的1指第一个参数,当前只有var一个可变参数,所以就是指var。
    * $后的0表示,位数不够用0补齐,如果没有这个0(如%1$nd)就以空格补齐,
    * 0后面的n表示总长度,总长度可以可以是大于9例如(%1$010d),d表示将var按十进制转字符串,长度不够的话用0或空格补齐。
    */
    @Test
    public void myTest1(){
    System.out.println(String.format("%1$01d", 1));
    System.out.println(String.format("%1$03d", 1));
    System.out.println(String.format("我叫%s,她叫%s", "小明","小方"));
    System.out.println(String.format("我叫%2$s,她叫%1$s", "小明","小方"));
    System.out.println(String.format("%o", 8)); //转变为八进制 10
    System.out.println(String.format("%x", 16)); //转变为16进制 10
    System.out.println(String.format("%d", 10)); //转变为10进制 10
    System.out.println(String.format("%1$,d", 12302562)); // 金额千分位隔开12,302,562
    System.out.println(String.format("%,d", 12302562)); // 金额千分位隔开12,302,562
    System.out.println(String.format("%1$.2f", 12.12555));// f表示传入的数字是浮点型,用于四舍五入保留两位小数12.13
    //调用String.format()方法是会new一个Formatter对象
    System.out.println(new Date());//Fri May 31 08:43:32 CST 2019
    System.out.println(String.format("%1$tY-%1$tm-%1$td", new Date()));//日期转换 2019-05-31
    System.out.println(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", new Date()));//日期转换 2019-05-31 08:45:24
    System.out.println(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS:%1$tL", new Date()));//日期转换 2019-05-31 08:45:24
    System.out.println(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %tA", new Date()));//日期转换 2019-05-31 08:49:52 星期五
    System.out.println(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS:%1$tL %1$tA %1$tp", new Date()));//日期转换 2019-05-31 08:49:52:034 星期五 上午
    System.out.println(String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS:%1$tL %1$tA %1$tp %tZ", new Date()));//日期转换 2019-05-31 08:49:52:034 星期五 上午 CST
    System.out.println(String.format("%tF", new Date()));//日期转换 2019-05-31
    System.out.println(String.format("%tD", new Date()));//日期转换 05/31/19
    System.out.println(String.format("%tc", new Date()));//日期转换 星期五 五月 31 08:53:58 CST 2019
    System.out.println(String.format("%tr", new Date()));//日期转换 08:54:21 上午
    System.out.println(String.format("%tT", new Date()));//日期转换 08:54:38
    System.out.println(String.format("%tR", new Date()));//日期转换 08:54
    }
    }
  • 相关阅读:
    光线步进——RayMarching入门
    MATLAB GUI制作快速入门
    Python中用绘图库绘制一条蟒蛇
    node 常见的一些系统问题
    webpack 入门指南
    利用 gulp 来合并seajs 的项目
    移动端 解决自适应 和 多种dpr (device pixel ratio) 的 [淘宝] 解决方案 lib-flexible
    富有哲理的文章
    NodeJS 难点(网络,文件)的 核心 stream 四: writable
    Vue.js 源码学习笔记 -- 分析前准备2 -- Object.defineProperty
  • 原文地址:https://www.cnblogs.com/liuyunche/p/14303433.html
Copyright © 2011-2022 走看看