java日期、数字格式化
java 日期格式化
Date d = new Date();
//简单的日期格式化工具类
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(d));
Date d = new Date();
System.out.printf("%tF %
//通过System类对象的currentTimeMillis() 方法返回当前系统的时间戳 是个long类型
long now =System.currentTimeMillis();
System.out.printf("%tF %
整数、小数格式化
package os.beiyou;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
public class format3 {
public static void main(String[] args) {
int i =10;
System.out.printf("%06d%n",i);
System.out.printf("%.4f%n",(float)i);
//修改系统
Locale.setDefault(Locale.US);
//currency 货币 根据系统是中文(英文),来加货币符号
NumberFormat ci = DecimalFormat.getCurrencyInstance();
//NumberFormat ci = DecimalFormat.getCurrencyInstance(Locale.US);
System.out.println(ci.format(i));
//百分比
NumberFormat nf = DecimalFormat.getPercentInstance();
System.out.println(nf.format(.3));
System.out.println(nf.format(.03));
//小数
// , 三位一逗号
System.out.printf("%.3f%n¥%<,.2f%n",12345685.14532f);
DecimalFormat df =new DecimalFormat("#,###.####");
System.out.println(df.format(123456.2232222));
}
}