第1个回答 2012-03-21
亲,直接转换就可以了!
涉及到的知识点如下:
1.SimpleDateFormat类是更改日期类字符串的专用模版。
yyyy年MM月dd日 hh:mm分.........................》yyyy-MM-dd HH:mm分
2.12小时用hh,24小时用HH
import java.text.*;
import java.util.Date;
public class DateFormatDemo {
public static void main(String[] args) {
SimpleDateFormat sp1 = new SimpleDateFormat("yyyy年MM月dd日 hh: mm分");
SimpleDateFormat sp2 = new SimpleDateFormat("yyyy-MM-dd HH: mm分");
try {
Date d = sp1.parse("2012年3月20日 23: 59分");
System.out.println(sp2.format(d));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
输出结果:
2012-03-20 23: 59分
第4个回答 2015-06-17
功能需求,输入一个字符串“1-2-3”切割出“1”、“2”、“3”。在Java下直接用String的split函数就可以了。c++下String没有直接提供这个函数,需要自己写。
[cpp] String recogScop = "01-02-03";
cout<<recogScop<<endl;
int size = recogScop.size();
int pos = 0;
string result[20] ;
for(int i=0, j=0; i<size; i++,j++ )
{
pos = recogScop.find("-", i);
if(pos == -1)
{
String subEnd = recogScop.substr(i, size - i); //最后一个字符串
result[j] = subEnd;
break;
}
if(pos >0)
{
String sub = recogScop.substr(i, pos-i);
result[j] = sub;
i = pos;
}
}
for(int i=0; result[i] != ""; i++)
cout<<result[i]<<endl;
String recogScop = "01-02-03";
cout<<recogScop<<endl;
int size = recogScop.size();
int pos = 0;
string result[20] ;
for(int i=0, j=0; i<size; i++,j++ )
{
pos = recogScop.find("-", i);
if(pos == -1)
{
String subEnd = recogScop.substr(i, size - i); //最后一个字符串
result[j] = subEnd;
break;
}
if(pos >0)
{
String sub = recogScop.substr(i, pos-i);
result[j] = sub;
i = pos;
}
}
for(int i=0; result[i] != ""; i++)
cout<<result[i]<<endl;
注意上面find的结果pos大于0,表示能够找到“-”分隔符,如果失败,也就是最后一个分隔符,pos会等于-1.