主题:String类型以Date方式显示
que1
[专家分:110] 发布于 2008-06-03 20:56:00
现有String类型变量date,其值为yyyy/mm/dd格式的日期,time为hh:mi:ss格式的时刻。
1) string[] dateArr = date.( );//将年、月、日分别取出放进数组
2) string[] timeArr = time.( );//将小时、分:秒分别取出放进数组,注意是①小时②分:秒两个元素
两括号该怎么添?
回复列表 (共4个回复)
沙发
lisy [专家分:390] 发布于 2008-06-04 00:11:00
import java.util.Calendar;
import java.text.SimpleDateFormat;
class Test2115
{
public static void main(String [] args)
{
Calendar calendar = Calendar.getInstance();
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy/MM/dd");
System.out.println(bartDateFormat.format(calendar.getTime()));
bartDateFormat = new SimpleDateFormat("HH:mm:ss");
System.out.println(bartDateFormat.format(calendar.getTime()));
}
}
板凳
sjhlovejava [专家分:1690] 发布于 2008-06-04 10:10:00
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String date = "2008/06/04";
String time = "01:02:03";
String[] strArrD = date.split("/");
String[] strArrT = time.split(":", 2);
for (int i = 0 ; i < strArrD.length; i++) {
System.out.println(strArrD[i]);
}
for (int i = 0 ; i < strArrT.length; i++) {
System.out.println(strArrT[i]);
}
}
}
3 楼
gp0014 [专家分:80] 发布于 2008-06-04 10:44:00
2楼已经解决,就是那样用split方法来匹配
4 楼
que1 [专家分:110] 发布于 2008-07-30 20:04:00
public class Test {
public static void main(String[] args) {
String date = "2008/06/03";
String time = "21:38:40";
String[] dateArr = date.split("/");
for(int i = 0; i < dateArr.length; i++) {
System.out.println("dateArr[" + i + "] = " + dateArr[i]);
}
System.out.println();
String[] timeArr = time.split(":(?=\\d\\d:)");
// String[] timeArr = time.split("(?<=^\\d\\d):"); // 两者相同
for(int i = 0; i < timeArr.length; i++) {
System.out.println("timeArr[" + i + "] = " + timeArr[i]);
}
System.out.println();
timeArr = new String[]{ time.substring(0, 2), time.substring(3) };
for(int i = 0; i < timeArr.length; i++) {
System.out.println("timeArr[" + i + "] = " + timeArr[i]);
}
}
}
我来回复