2017-01-04 2 views
0

내 DateTimeFormat 패턴으로 현재 날짜 시간을 얻기 위해 노력하고있어,하지만 난 예외를 받고 있어요 ...Joda 날짜 시간 잘못된 포맷

//sets the current date 
DateTime currentDate = new DateTime(); 
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale); 
DateTime now = dtf.parseDateTime(currentDate.toString()); 

나는이 예외를 받고 있어요, 나는 사람이 이해할 수없는 당신이 기본 toSring 형식과 구문 분석 날짜를 시도하기 때문에 잘못된 형식

java.lang.IllegalArgumentException: Invalid format: "2017-01-04T14:24:17.674+01:00" is malformed at "17-01-04T14:24:17.674+01:00" 
+0

당신은 문자열로 날짜를 포맷하려고하거나 날짜 시간 객체로 문자열을 구문 분석하려고 체크 아웃 구문 분석이야? – assylias

+0

DateTime 객체 여야합니다 – besmart

+0

하지만 'DateTime'으로 시작하는 * 이유는 텍스트로 변환하고 다시 변환하는 이유는 무엇입니까? –

답변

2

에게이 줄 DateTime now = dtf.parseDateTime(currentDate.toString());을 제공하는 것은 올바르지 않습니다. 패턴과 같은 형식으로 포맷 된 문자열을 구문 분석해야합니다.

DateTime currentDate = new DateTime(); 
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale); 
String formatedDate = dtf.print(currentDate); 
System.out.println(formatedDate); 
DateTime now = dtf.parseDateTime(formatedDate); 
System.out.println(now); 
1

잘못된 형식을 사용하여 날짜를 구문 분석하고 있습니다. 당신이 toString의 문자열로 변환 한 후 분석하고자하는 날짜를 인쇄 할 경우 얻을 :

2017-01-04T14:24:17.674+01:00 

이 날짜 문자열 패턴 dd/MM/YYYY HH:mm을 준수하지 않습니다. 당신에게 원래 currentDate 같은 시간을 나타내는 또 다른 인스턴스를 얻을 것이다

DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ") 
             .withLocale(locale); 

DateTimeFormatter와 구문 분석 : 문자열로 다시 DateTime 객체에 currentDate 변환 구문 분석하려면 다음과 같은 패턴을 사용합니다.

DateTimeFormatter에 대한 자세한 내용은

그리고 옵션은 JavaDoc