How to convert java.sql.Date to java.util.Date in dd/MM/yyyy format?

你说的曾经没有我的故事 提交于 2019-12-10 11:56:16

问题


I am storing a date to database by converting util date to sql date by using following code

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");  
sdf.setLenient(false);
java.sql.Date dobSQLDate = null;
Date date = null;
if(!("").equals(userDob)){date = sdf.parse(userDob);dobSQLDate = new java.sql.Date(date.getTime());}

now I want to show this date on my page in the dd/mm/yyyy format in which it was taken... How do I convert this?


回答1:


you can use the format method on your SimpleDateFormat object. It takes a java.util.Date object and returns a String formatted based on the format string specified.




回答2:


you can use DateFormatUtils

//Formats you dateto a specific pattern
 DateFormatUtils.format(yourDate, "dd/MM/yyyy");



回答3:


Dates do not have a "format" that you can set - they are just an instant in time.

If you want to display a date in a certain format, you must render the date to a String using code such as:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");  
String dateString = sdf.format(date);


来源:https://stackoverflow.com/questions/14344373/how-to-convert-java-sql-date-to-java-util-date-in-dd-mm-yyyy-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!