比如我在Java程序中得到了一个字符串类型 值时“2016-08-17”。
我想把这个字符串转出日期类型,值还是“2016-08-17”,只是把类型改成了Date型,
我用SimpleDateFormat转换后得到了 Sun Jan 17 00:08:00 CST 2016,
但是我想得到值显示为“2016-08-17”,怎么弄
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFmt=new SimpleDateFormat("yyyy-MM-dd");
System.out.println(myFmt);
再format成你要的格式就可以了。
String s = "2016-08-17";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date d = sdf.parse(s);
System.out.println(sdf.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
//原日期字符串
String str = "2016-08-17";
//new一个带日期类型的SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//调用SimpleDateFormat的parse方法,就得到了2016-08-17的日期类型
Date d = sdf.parse(s);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format(sdf.parse("2016-8-19")));