版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ljh_learn_from_base/article/details/84782392
/**
*
*/
package com.ljh.myo2o;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
/**
* @description:
* @author :ljh
* @CreateTime:2018-12-03 07:42:20
*/
public class MainMethodTest {
//详细的String格式化语法可以参照以下文档,在这里本coder就抛转引玉了
//https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax
public static void main(String[] args) {
//先写出%s,然后在%于s之间填写第几个引用,用n$表示,其中n代表数字
System.out.printf("%1$s,%1$s%2$s!", "Hello","World");
System.out.println("----如果想取出使用了printf方法格式后的字符串,可以使用以下方法-----");
Formatter formatter=new Formatter();
String afterFormat = formatter.format("%1$s,%1$s%2$s!", "Hello","World")/*.out()*/.toString();
System.out.printf("afterFormat is %s%n",afterFormat);
//格式化日期
System.out.printf("%tF",LocalDateTime.now());
//格式化日期 也可以使用System.out.format
System.out.format("%tF",LocalDateTime.now());
//这里String提供格式化的第二种方法
String format = String.format("%tF", LocalDateTime.now());
//当然如果想格式化日期,java8以后 提供了DateTimeFormatter
}
}