aspose file tools*
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
JavaRanch » Java Forums » Java » JSP
Bookmark "Date format in JSP" Watch "Date format in JSP" New topic
Author

Date format in JSP

Santosh Kumar Nayak
Ranch Hand

Joined: Aug 02, 2011
Posts: 82
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();
System.out.println(dateFormat.format(date));


Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));


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...!!!

Santosh.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 761
    
    9
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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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: http://aspose.com/file-tools
 
subject: Date format in JSP
 
Similar Threads
date problem
How do I get date and time seperate from Date Object
how to change the date,Time format after accessing it from the database
xml date conversion all date conversions
System Date