Solved

date format

Posted on 2004-10-10
1
844 Views
Last Modified: 2011-09-20
I actually have 3 questions with date formats , can someon tell me how to do following

1) I need to display just current month and year on my JSP, like for this month display should be exactly "October, 2004. "  , also i have to display with date at an other place like "October, 10 2004"

2) I get Date as a string in format (MM/DD/YYYY)e.g 10/10/2004. I need to convert it again to just Month and Year format  like October, 2004.

3) Same as question 2, but in this case i get java.util.Date object instead of String.
I think these are easy but i have never worked with dates and formats..
Thanks in advance
0
Comment
Question by:soft_techy
1 Comment
 
LVL 21

Accepted Solution

by:
MogalManic earned 50 total points
ID: 12272346
You will need to work with the SimpeDateFormat class.
  1) Convert today's date to Month, Year:
       <%
        Date today=new Date();
        SimpleDateFormat format=new SimpleDateFormat('MMMM, yyyy");
        %>
...
        <%-format.format(today);%>
  2) Convert a date string to a date Object (ignoring Day)
       <%
        String dateStr=request.getParameter("enteredDate");
        SimpleDateFormat format=new SimpleDateFormat('MM/xx/yyyy");
        Date date=format.parse(dateStr);
        %>
   3) Ignore day from Date object
       <%
        Date today=new Date();
        SimpleDateFormat format=new SimpleDateFormat('MMMM, yyyy");
        String dateStr format.format(today);    //Convert date to string
        today=format.parse(dateStr);             //Convert String to date
        %>
        OR
       <%
             Date today=new Date();
             Calendar dt=Calendar.newInstance();
             dt.set(Calendar.DAY, 1);   //Set the month to 1st of the month
             dt.clear(Calendar.HOURS);  //Remove the time
             dt.clear(Calendar.MINUTES);
             dt.clear(Calendar.SECONDS);
             dt.clear(Calendar.MILLISECONDS);
             today=dt.getTime();  //Today now is the first of the current month
      %>
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IE doesn't Invoke servlet in iframe 1 111
Books that can get me started on JAVA 2 95
eclipse buid path vs tomcat lib path 10 24
Unhandled exception type Exception 18 31
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
Adding Computers to AD groups through an SCCM Task Sequence
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question