怎么将String切割

2012年3月20日 23:59分 要怎样将这个String类型的日期切割,然后拼接成2012-3-20 23:59分,求赐教~~

  将String切割的方法如下:
  1、用竖线 | 分隔字符串,
  String[] aa ="aaa|bbb|ccc".split("|");
  //String[] aa = "aaa|bbb|ccc".split("\\|"); 这样才能得到正确的结果
  for(int i =0; i <aa.length ; i++){
  System.out.println("--"+aa);
  }
  2、用竖 * 分隔字符串运行
  String[] aa ="aaa*bbb*ccc".split("*");
  //String[] aa = "aaa|bbb|ccc".split("\\*"); 这样才能得到正确的结果
  for(int i =0; i <aa.length ; i++){
  System.out.println("--"+aa);
温馨提示:答案为网友推荐,仅供参考
第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分
第2个回答  2012-03-21
public static void main(String[] args) throws Exception{
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年M月dd日 HH:mm分");
Date date = sdf1.parse("2012年3月20日 23:59分");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-M-dd HH:mm分");
System.out.println(sdf2.format(date));
}
第3个回答  2012-03-21
.split("\\D+")
得到数组了,自己再组合。本回答被提问者采纳
第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.
相似回答