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 %<tT %<tc %1$tA%n",d);
String str = String.format("%tY%<tm%<td%<tH%<tM%1$tS.jpg%n",d);
System.out.println(str);
long now =System.currentTimeMillis();
System.out.printf("%tF %<tT %<tc %1$tA%n",now);
System.out.printf("%tF %<tT %<tc %1$tA%n",System.currentTimeMillis());
整数、小数格式化
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);
NumberFormat ci = DecimalFormat.getCurrencyInstance();
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));
}
}
