Solved

date format

Posted on 2004-10-10
1
839 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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now