站内搜索: 请输入搜索关键词

当前页面: 开发资料首页JSP 专题有什么函数可以将2006-9-5变换成200609的转变呢,请帮忙

有什么函数可以将2006-9-5变换成200609的转变呢,请帮忙

摘要: 有什么函数可以将2006-9-5变换成200609的转变呢,请帮忙


有什么函数可以将2006-9-5变换成200609的转变呢,请帮忙


不知你所云,你是不是想截取把时间字符串?
String timeStr = "2006-9-5";
String year = "";
year = timeStr.substring(0,4);
String month = timeStr.substring(timeStr.indexOf("-"),timeStr.lastIndexOf("-")) ;
if(!month.startsWith("0")){
month = "0"+month;
}
timeStr = year+month;


转Date类型,再用转yyyy-MM就可以了.


怎么转呢,能详细些吗


DateFormat df1 = new SimpleDateFormat("yyyy-M-d");
Date date = df1.parse("2006-9-5");
DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
return df2.format(date);


方法同上.

第三行改成:
DateFormat df2 = new SimpleDateFormat("yyyy-MM");


if(!month.startsWith("0")){
month = "0"+month;
}

那 month 是10怎么办 那是不是就成了"010"了
按这个思路 我觉得可以写成

if(Integer.parseInt(month)<10){
month = "0"+month;
}


DateFormat不能解析


String str2=null;
String str="2006-9-5";
String str1=str.substring(0,6);
SimpleDateFormat ss= new SimpleDateFormat("yyyyMM");
try{
str2=sdf.format(sdf.parse(str1));
}catch(ParseException e){}


String str2=null;
String str="2006-9-5";
String str1=str.substring(0,6);
SimpleDateFormat ss= new SimpleDateFormat("yyyyMM");
try{
str2=sdf.format(sdf.parse(str1));
}catch(ParseException e){}
=================sdf是什么来的=========


哦,不好意思,SimpleDateFormat sdf= new SimpleDateFormat("yyyyMM");


tigerwithwing() 正解,楼主去试试吧
goodluck


↑返回目录
前一篇: 关于Session监听
后一篇: 大虾们,请教一下JSP页面这种情况可能是什么问题导致的