• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date format in JSP

 
Ranch Hand
Posts: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28245
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic