每当我要解析日期时,我都无法获得yyyy-MM-dd格式的日期。
下面是我的代码
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo {
public static void main(String[] args) throws Exception {
Demo d=new Demo();
System.out.println(d.getDate1("2011-08-11"));
}
public Date getDate1(String pValue) throws Exception{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.parse(pValue);
}
}输出:
Thu Aug 11 00:00:00 IST 2011.这里我将字符串数据格式转换为日期格式。
发布于 2015-12-18 20:00:23
当您像这样打印date对象时:
System.out.println(d.getDate1("2011-08-11"));然后,只需调用默认的toString()方法date即可。如果要打印格式化的日期,则应使用日期格式化程序。
示例:
public class Main {
public static void main(final String[] args) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(dateFormat.format(dateFormat.parse("2011-08-11")));
}
}发布于 2015-12-18 19:59:08
类Date表示时间上的特定瞬间,精度为毫秒。当您打印它时,您没有将其格式化为仅打印年、月和日,因此它将获得默认格式。IST是时区。
https://stackoverflow.com/questions/34355036
复制相似问题