File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Capture month and year from user input date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Capture month and year from user input date" Watch "Capture month and year from user input date" New topic
Author

Capture month and year from user input date

Lalit Vora
Ranch Hand

Joined: Jun 22, 2006
Posts: 37
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Lalit Vora
Ranch Hand

Joined: Jun 22, 2006
Posts: 37
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

Joined: Jun 22, 2006
Posts: 37
Hi,

Thank you for your support. It is done as per your suggestion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Capture month and year from user input date