java SimpleDateFormat
答案:2 悬赏:0 手机版
解决时间 2021-01-28 17:13
- 提问者网友:王者佥
- 2021-01-27 23:39
java SimpleDateFormat
最佳答案
- 五星知识达人网友:你哪知我潦倒为你
- 2021-01-28 00:14
public static void main(String[] args) {
String str = "2010-1-10 17:39:21";
SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
try {
System.out.println(format1.format(format.parse(str)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String str = "2010-1-10 17:39:21";
SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd HH:mm:ss");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
try {
System.out.println(format1.format(format.parse(str)));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
全部回答
- 1楼网友:平生事
- 2021-01-28 00:53
String s = "2010-1-10 17:39:21";
//SimpleDateFormat format1 = new SimpleDateFormat("yyyyMddHHmmss");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMddHHmmss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format1.format(format2.parse(s)));
这个很灵活的,你想要什么样的就改成什么样就行了。
要占俩位就MM ,不需要前面补0的话就 M 就行,你自己试试吧,你那个之所以得到的值不对是因为把字符串转成日期时用的格式跟你的格式不符的关系。
//SimpleDateFormat format1 = new SimpleDateFormat("yyyyMddHHmmss");
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMddHHmmss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format1.format(format2.parse(s)));
这个很灵活的,你想要什么样的就改成什么样就行了。
要占俩位就MM ,不需要前面补0的话就 M 就行,你自己试试吧,你那个之所以得到的值不对是因为把字符串转成日期时用的格式跟你的格式不符的关系。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯