• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date format in JSP

 
Santosh Kumar Nayak
Ranch Hand
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20742
30
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic