formatting date to dd-mm-yyyy in jsp

2019-07-28 21:57发布

I need date format in dd-mm-yyyy format in jsp.My database(dd/mm/yyyy-having date datatype in db) is read to jsp(using form bean, CmFrm.getExpireDate() - having datatype as string).
It is now read as yyyy-mm-dd hh:mm:ss.a in jsp i want it to read in dd-mm-yyyy format.
I have tried using --

String txtdate =CmFrm.getExpireDate(); 
SimpleDateFormat pd = new SimpleDateFormat("dd/MM/yyyy");  
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
Date date = (Date) pd.parse(txtdate); 
String DisplayDate= df.format(date); 
System.out.println("MyExpireDate.."+DisplayDate); 

I get unparsable error.

I have applied format function alone as -

String txtdate =CmFrm.getExpireDate(); 
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");  
System.out.println("Date.."+df.format(txtdate));

here jsp goes blank.
please help as how to display date on jsp in dd-mm-yyyy.

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-28 22:37

I tried your first code and I don't get any exceptions:

String txtdate = "06/06/2012"; 
SimpleDateFormat pd = new SimpleDateFormat("dd/MM/yyyy"); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 

Date date = null;
try {
    date = (Date) pd.parse(txtdate);
} catch (ParseException e) {
    e.printStackTrace();
} 

String DisplayDate= df.format(date); 
System.out.println("MyExpireDate.."+DisplayDate);

When I execute the following, I get:

MyExpireDate..2012-06-06

Maybe you've imported the wrong date libraries? I imported these:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
查看更多
一夜七次
3楼-- · 2019-07-28 22:50

Should do you good...

String MyDate = CmFrm.getExpireDate();
SimpleDateFormat parseDate = new java.text.SimpleDateFormat("dd/MM/yyyy");
SimpleDateFormat formatDdate = new java.text.SimpleDateFormat("yyyy-MM-dd");
Date date = (Date) parseDate.parse(MyDate);
String DisplayDate= formatDate.format(date);

Output example: 2016-11-19

查看更多
登录 后发表回答