Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes VERY urgent help me please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "VERY urgent help me please" Watch "VERY urgent help me please" New topic
Author

VERY urgent help me please

koray guclu
Ranch Hand

Joined: Feb 23, 2001
Posts: 59
Hi,
I have a project I use locales in that project to format the dates. I must use mm.dd.yyyy pattern. I use MSSQL server. I get the dates in dd.mm.yyyy format. There is not any problem when I get this and format this.
But I have a problem when I try to insert or update dates. Server excepts dd.mm.yyyy format. and end users enters the dates in mm.dd.yyyy format I used the following structure in my JSP page
I want to convert mm.dd.yyyy format to dd.mm.yyyy format. I get the start_date as a parameter of JSP page
----------------------
Locale currentLocale = new java.util.Locale("en", "US");
DateFormat currentDateFormatter = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, currentLocale);
String start_date=request.getParameter("StartDate");
System.out.println(start_date);
SimpleDateFormat formatter = new SimpleDateFormat ("mm.dd.yyyy");
start_date = formatter.format(new java.util.Date(start_date));
start_date=currentDateFormatter.format(new java.util.Date(start_date));
----------------------
Thanks,

-----------------------------------------------------<BR> Koray GUCLU ~ the sky has no limit ~<BR> (B.s. Computer Engineer)<BR> Frankfurt am Main, Germany<BR> web : <A HREF="http://www.geocities.com/korayguclu/<BR rel="nofollow">" TARGET=_blank>http://www.geocities.com/korayguclu/<BR></A> mail: korayguclu@yahoo.com<BR>-----------------------------------------------------
Ganesh Anekar
Ranch Hand

Joined: May 13, 2001
Posts: 36
Hello,
As per u r code , u r getting start_date...after getting that date .Take that date in on string and use substring function ..cut that into mm , dd , yyyy seperately and then arrange it while inserting into database...
Try...
Ganesh
kichu kichu
Ranch Hand

Joined: May 10, 2001
Posts: 59
Hai,
Below is my Code to convert yyyy/mm/dd to mm/dd/yy format
int yt1 = Integer.parseInt(strReqDate.substring(0,4)); //Retrieve Year
int mt1 = Integer.parseInt(strReqDate.substring(5,7)); //Retrieve Month
int ddt1 = Integer.parseInt(strReqDate.substring(8,10));//Retrieve Date
//Code to convert yyyy/mm/dd to mm/dd/yy format
if ( (mt1>0) && (mt1<=9) )
{
strRD = "0" + mt1 + "/" + ddt1 + "/" + yt1;
}
if ( (ddt1>0) && (ddt1<=9) )
{
strRD = mt1 + "/" + "0" + ddt1 + "/" + yt1;
}
if ( (mt1<=9) && (ddt1<=9) )
{
strRD = "0" + mt1 + "/" + "0" + ddt1 + "/" + yt1;
}
if ( (mt1>9) && (ddt1>9) )
{
strRD = mt1 + "/" + ddt1 + "/" + yt1;
}
change this according to your requirements.I have done this in my project and it works.
At the end ,u will have to insert strRD into the database
"insert into table(DDATE) values('"+strRD+"')"
hope u will make the necessary changes.
bye,
kichu.

Poornima Shetty
Greenhorn

Joined: Dec 12, 2000
Posts: 26
Hi,
<html>
<head>
<script language="javascript">
//validation for 4 digit Year
function isValidDate(dateVar)
{
var mon = new Array(12);
mon[1] = "January";
mon[2] = "February";
mon[3] = "March";
mon[4] = "April";
mon[5] = "May";
mon[6] = "June";
mon[7] = "July";
mon[8] = "August";
mon[9] = "September";
mon[10] = "October";
mon[11] = "November";
mon[12] = "December";
dateStr = dateVar;
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null)
{
alert("Not in a valid format. Please Enter (dd/mm/yyyy) format")
return false;
}
day = matchArray[1];
month = matchArray[3];
year = matchArray[4];
if(year=="0000")
{
alert("Year Cannot be Zero");
return false;

}
if (month < 1 | | month > 12)
{
alert("Month must be between 1 and 12 ");
return false;
}
if (day < 1 | | day > 31)
{
alert("Day must be between 1 and 31" );
return false;
}
if ((month==4 | | month==6 | | month==9 | | month==11) && day==31)
{
alert("Month "+mon[eval(month)]+" doesn't have 31 days!");
return false;
}
if (month == 2)
{
// check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 | | year % 400 == 0));
if (day>29 | | (day==29 && !isleap))
{
alert( "February, " + year + " doesn't have " + day + " days!");
return false;
}
}
alert("Entered Date is finally Correct");
return true; // date is valid
}
</script>
</head>
<body>
<form name="DateValidation" method="post">
<input type="text" name="Date" >
<input type="button" name="click" value="Click to Validate Date" onclick="javascript:isValidDate(document.forms[0].Date.value)">
</form>
</body>
</html>

Well this above function as you see validates for not only dd/mm/yyyy date format but also checks for the following:
1.Leap Year validation
2.Checks that year cannot be entered as 0000.
3.Checks for feb month which cannot have 30 or 31 days.
4.checks for months that dont have 31 days in them.
5.checks for day value not to be between 1 and 31
6.checks for month value to be between 1 and 12.
I hope the above code helps you ,all you got to do is cut paste the code from <html> to </html> into a page and
save it as a html and test it.Your requirement is 100% in this page.
Regards,
........P.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: VERY urgent help me please