我现在在做jsp的练习
环境 eclipse+oracle
从库里查出的日期在oracle里日期格式是: 01-一月 -1985 12:00:00 AM
现在想把得到的日期型变量的月取出来,由页面显示。
Date birthdate=rs.getDate( "birthdate ");
out.println( " </SELECT> <SELECT name= 'month '> ");
for(int i=1;i <13;i++){
if(i==birthdate.getMonth())
out.print( " <OPTION selected value= "+i+ "> "+i+ "月 </OPTION> ");
else
out.print( " <OPTION value= "+i+ "> "+i+ "月 </OPTION> ");
}
可是用 birthdate.getMonth() 报错 ,且出不来预期的效果。
我想请问各位高人,该怎么取日期变量里的 年月日 呢?
谢谢
------解决方案--------------------
转化为Calendar对象再由Calendar.get(Calendar.Month)获得试试
------解决方案--------------------
java.text
类 DateFormat
java.lang.Object
继承者 java.text.Format
继承者 java.text.DateFormat
lz可以去查看java.text.DateFormat类
里面有详尽的方法来解决有关date的格式转化问题!!!
------解决方案--------------------
import java.util.GregorianCalendar;
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(birthdate);
String val = cal.get(GregorianCalendar.YEAR) + "- "
+ (cal.get(GregorianCalendar.MONTH) + 1) + "- "
+ cal.get(GregorianCalendar.DAY_OF_MONTH);