Solved

date format

Posted on 2004-10-10
1
847 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
report generation frameworks 1 70
sortaSum challenge java 17 174
Books that can get me started on JAVA 2 117
mysql jsp example issue 32 71
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
Read our guide on how to survive being on-call.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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