我有一个类似的字符串"2015-07-16 17:07:21".我想将其转换为相同格式的日期.我试过这样的事情:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
Date date = sdf.parse("2015-07-16 17:07:21");
Run Code Online (Sandbox Code Playgroud)
但输出的格式不同Thu Jul 16 17:00:21 IST 2015.我怎样才能让它按需要运行.谁能帮我.我知道这可能是重复但我没有找到任何运气.
我有Date格式的Date对象
/107/2013 12:00:00 AM
Run Code Online (Sandbox Code Playgroud)
我的预期价值:
2013-07-01
Run Code Online (Sandbox Code Playgroud)
我怎么做的?
我正在尝试使用此代码
public static Date formatearFecha( Date fecha ){
String fechaString = new SimpleDateFormat("yyyy-MM-dd").format(fecha) ;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date fechaFormateada = null;
try {
fechaFormateada = df.parse(fechaString);
} catch (ParseException ex) {
Logger.getLogger(TestThings.class.getName()).log(Level.SEVERE, null, ex);
}
return fechaFormateada;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试进行测试时,我得到了这个:
System.out.println( formatearFecha(myDate) );
Mon Sep 30 00:00:00 COT 2013
Run Code Online (Sandbox Code Playgroud)
更新:
我的问题是在sql中将java.util.Date更改为java.sql.Date
我解决了这个问题:
private String formatearFecha( Date fecha ){
return new SimpleDateFormat("yyyy-MM-dd").format(fecha);
}
private java.sql.Date stringToSQLDate( String fecString ){
java.sql.Date fecFormatoDate = null; …Run Code Online (Sandbox Code Playgroud)