Reputation: 2454
I've found myself working on some old JSP and want to do something simple like display today's date in dd/mm/yyyy format
Doesn't seem to be that simple,
So far I've imported java.util.*
And I've tried various things like
String df = new SimpleDateFormat("dd/MM/yy");
However, to no avail...
Upvotes: 2
Views: 48964
Reputation: 131
d will stores the current date only...it works
<%
long millis=System.currentTimeMillis();
java.sql.Date d=new java.sql.Date(millis);
%>
Upvotes: 0
Reputation: 94643
A cleaner approach is to use JSTL
- <fmt:formatDate/>
tags.
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<jsp:useBean id="now" class="java.util.Date"/>
<fmt:formatDate value="${now}" dateStyle="long"/>
<fmt:formatDate value="${now}" pattern="dd-MM-yyyy HH:mm:ss a z" />
Upvotes: 16
Reputation: 10093
You can use this:
Calendar now = Calendar.getInstance();
and then use some of the Calendar fields as needed, e.g.:
int dayOfMonth = now.get(Calendar.DAY_OF_MONTH);
String dayOfMonthStr = ((dayOfMonth < 10) ? "0" : "") + month;
int month = now.get(Calendar.MONTH) + 1;
String monthStr = ((month < 10) ? "0" : "") + month;
System.out.print(dayOfMonthStr+"/"+monthStr+"/"+now.get(Calendar.Year));
To follow your original idea, I think you should do something like:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy");
sdf.format(new Date());
Upvotes: 4
Reputation: 1
First, you have to set date as
Date dNow = new Date( );
This coding helps to call date. Now, we have to set date format with coding as
new SimpleDateFormat ("dd.MM.yyyy");
This is the simple coding for date. If you need to make any other changes in font and position then you can do it.
Upvotes: 0
Reputation: 3948
Based on your given information you have imported java.util.* libraries. This is not enough. If you want using SimpleDateFormat you have to imported it. Add
<%@page import="java.text.SimpleDateFormat" %>
to the top of your jsp file. After that define the format what you want to get
Example: <%! final static String DATE_FORMAT_NOW = "dd/MM/yy"; %>
After that try
<%
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT_NOW);
%>
Upvotes: 1