밀리 초 단위로 날짜를 나누고 내 형식으로 인쇄하려고하지만 색인이 범위를 벗어났습니다. 분할 ("/")의 경우에는 작동하지만 분할 (".")의 경우에는 작동하지 않습니다.문자열 분할 기능이 원하는대로 작동하지 않습니다.
왜 이런 일이 발생하는지 알 수 없습니다.
코드는 다음과 같습니다
public class c {
public static void main(String[] arg)
{
Date date=new Date();
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.FFF");
System.out.println(formatter.format(date));
String a=formatter.format(date);
String b[]=a.split(" ")[0].split("/");
String x1=(Integer.parseInt(b[2])-2000)+b[1]+b[0];
System.out.println("date part is : "+x1);
String c[]=a.split(" ")[1].split(":");
System.out.println(c[0]);
System.out.println(c[1]);
System.out.println(c[2]);
System.out.println(c[2].trim().split(".")[0]);// exception at this line
System.out.println(c[2].trim().split(".")[1]);
String x2=c[0]+c[1]+c[2].split(".")[0]+c[2].split(".")[1]+"";
System.out.println("time part is : "+x2);
}
}
로그는 다음과 같습니다
08/10/2013 12:02:18.002
date part is : 131008
12
02
18.002
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:0 at c.main(c.java:22)
@Tichodroma가 지적한대로 "". ""을 "\\."'트릭을해야하지만 ... ... 코드가 끔찍합니다. ! 왜 당신은 모든 연결과 분할 jugglings을하는 것을 괴롭 히며, 당신은'Date' 클래스에서 사용할 수있는 매우 간단한 접근자를 가지고 있습니까? –
@ Orabîg'Date'의 접근자는 모두 사용되지 않습니다. –
음, 좋아,하지만 SimpleDatFormat을 사용하고 결과 문자열에서 정보를 추출하십시오. 기본 Data 접근자를 사용하는 것보다 더 좋은 아이디어가 있습니까? 나는 그렇게 생각하지 않는다. (imho) –