This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes JSP and the fly likes Date format in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Date format in JSP" Watch "Date format in JSP" New topic

Date format in JSP

Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 141
Hi Team,

My requirement is to display the date and time on a JSP page. The issue here is date is stored in a constant variable in the form YYYYMMDDHHMMSS

I tried the following code:-

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class GetCurrentDateTime {
public static void main(String[] args) {

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

Date date = new Date();

Calendar cal = Calendar.getInstance();

Can we make changes in the code to take the date as Input and show the output in the format yyyy-MM-dd HH:mm:ss

Any Ideas would be highly appreciated...!!!

E Armitage

Joined: Mar 17, 2012
Posts: 989
Santosh Kumar Nayak wrote:..The issue here is date is stored in a constant variable in the form YYYYMMDDHHMMSS

Do you mean that the date is in a String variable in that format?

If so then you must use the parse method to parse it into a java.util.Date first then format the Date into a String of the required format.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19899

In a JSP, the natural way to format a date is to use JSTL's <fmt:formatDate> element. There's also a <fmt:parseDate> element to parse a string into a date. Both of them support anything that the SimpleDateFormat object supports.

It looks to me like you would need both of those elements; <fmt:parseDate> to parse your string into a date based on one pattern, then <fmt:formatDate> to format it for output using a different pattern.
I agree. Here's the link:
subject: Date format in JSP
It's not a secret anymore!