Java转换成特定字符串

1.把Integer类型转换成特定格式的字符串类型,
例子:
Integer startTime;//1234→001234
DecimalFormat decimalFormat = new DecimalFormat(“000000”);//”000000” 为字符串格式,也可以转换成其他类型
String startTimeString = decimalFormat.format(startTime);
2.时间类型转换成特定字符串类型
例子:
Date date = new Date();
SimpleDateFormat simple = new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
String stringDate = simple .format(date);

Date date = new Date();
SimpleDateFormat simple = new SimpleDateFormat(“yyyy-MM-dd”);
String stringDate = simple .format(date);
3.字符串转换成时间类型
String stringDate= “2018-07-31”;
SimpleDateFormat simple = new SimpleDateFormat(“yyyy-MM-dd”);
Date date = simple .parse(stringDate);

String stringDate= “20180731”;
SimpleDateFormat simple = new SimpleDateFormat(“yyyyMMdd”);
Date date = simple .parse(stringDate);

猜你喜欢

转载自blog.csdn.net/qq_41332396/article/details/81296725