Solved

date format

Posted on 2004-10-10
1
850 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

626 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