This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Capture month and year from user input date

 
Lalit Vora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am developing struts appication. I am displaying date in index.jsp as mm/dd/yyyy format. User can enter date in the same format. After Validation i am receiving date in mm/dd/yyyy from user. Now from mm/dd/yyyy i want to capture only month and year as i want to show data from mm/01/yyyy mm/dd/yyyy. Example if user input 10/12/2005 then i need to show data from 10/01/2005 to 10/12/2005 So i want to capture montha dn year what user enters. Can anyone guide me how to get month and year from user input date.

Thanks
Lalit
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you've been through validation and you're confident that the string is in mm/dd/yyyy format you could substring the fields or use String.split() to break the string into an array.

It's more "objecty" though to use SimpleDateFormat to parse the string into a Date object, then set the day-of-month field to 01, then use the date format again to convert it to a new string.

I don't know your comfort level with exploring either of those solutions. Let us know if you've used SimpleDateFormat or can figure it out from the JavaDoc. Put together a code fragment that shows us how far you get and where you get stuck - if you do.
 
Lalit Vora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String dateFormat = "MM/dd/yyyy";
int dateFormatLength = dateFormat.length();
try {
if (recdt.length() != dateFormatLength)
throw new Exception();
else {
SimpleDateFormat format = new SimpleDateFormat(dateFormat);
format.setLenient(false);
Date theDate = new Date();
theDate = format.parse(recdt);
a =0;
System.out.println("valid date" +a);
}
}
catch(Exception e)
{
a = 1;
System.out.println("invalid date" +a);
if (a==1)
{
errors.add("Recdt", new ActionError("error.recdt"));
}

}
I have used this in validation. And i have date from user input in recdt

private String recdt = null;
public String getRecdt() {
return (this.recdt);
}
public void setRecdt(String recdt) {
this.recdt = recdt;
}

I am not getting what u r saying exactly as i am new to java can you please help me or modify code somewhere Its little urgent.

Thank you for your support
 
Lalit Vora
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thank you for your support. It is done as per your suggestion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic