• Post Reply Bookmark Topic Watch Topic
  • New Topic

Capture month and year from user input date  RSS feed

 
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
 
(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.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!