user3754716
user3754716

Reputation: 21

JSP format date and time

I am creating one module in jsp, in which I have to show auto generated date & time on the page.

Date & Time is generated but it is in MM/DD/YY - HH/MM/SS i.e. 10/23/2014 - 15:15:22 format . But i want to display it as 23/10/2014 - 03:15 PM

I have to format the time in my JSPcode.

Anybody have any idea/code for this?

Upvotes: 0

Views: 4333

Answers (2)

Braj
Braj

Reputation: 46881

In JSP, you can use JSTL fmt tag library that provides a set of tags for parsing and formatting locale-sensitive numbers and dates.

Read more Oracle Tutorial - Internationalization Tag Library and JSP Standard Tag Library

Sample code:

<c:set value="10/23/2014 - 15:15:22" var="dateString" />

<fmt:parseDate value="${dateString}" var="dateObject"
                                      pattern="MM/dd/yyyy - HH:mm:ss" />

<fmt:formatDate value="${dateObject}" pattern="dd/MM/yyyy - hh:mm a" />

Upvotes: 1

Bobz
Bobz

Reputation: 2624

Use java.text.SimpleDateFormat to format to any pattern you want!

Date date = new Date(); 
SimpleDateFormat dt1 = new SimpleDateFormat("dd/MM/yyyy '-' hh:mm a");
System.out.println(dt1.format(date));

More info: http://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html

Upvotes: 0

Related Questions