0

Dear all,
I have textbox (txtTransDate) that I set text to '2008-04-17' because mySQL accepts that format date.
I want to change the format to "MM/dd/yyyy" 04-17-2008 to retrieve month and year.

But the error is like this :
java.text.ParseException: Unparseable date: "2008-04-17"

int tahun =0, bln=0 ;
String sBln="";
SimpleDateFormat dtChange = new SimpleDateFormat("MM/dd/yyyy");
try
{
Date date = dtChange.parse(txtTransDate.getText().trim());
tahun = date.getYear() + 1900;
bln = date.getMonth() + 1;
sBln = Integer.toString(bln);
if(sBln.length()==1)
{
sBln = "0" + sBln;
}
}
catch(Exception e)
{
System.out.println(e.toString());
}

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by jinternalframe
1

Hi,

First of all, if you are referring to java.sql.Date or java.util.Date, then the methods getYear() and getMonth() are deprecated. Use Calendar.get(Calendar.DAY_OF_MONTH) and Calendar.get(Calendar.MONTH) instead.

SimpleDateFormat dtChange = new SimpleDateFormat("MM/dd/yyyy");	
java.util.Date date = new SimpleDateFormat("yyyy-mm-dd").parse(txtTransDate);	
String dt = dtChange.format(date);

Now, date holds the original date, dt has the date string formatted as you wanted. Play!

Votes + Comments
Thanks for your help
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.