2014-05-20 6 views
0

나는이 코드를 웹 페이지에서 추출한 문자열을 날짜 형식으로 변환하도록 작성했습니다. 하지만 실행할 때마다 월이 01로 변환됩니다.문자열을 날짜로 변환하는 함수

이 코드를 디버깅 할 수 있습니까?

if (this.subject != null && !this.subject.isEmpty()) { 
     DateFormat userDateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
     Date df = DATE_FORMAT.parse(this.subject); 
     Calendar cal = Calendar.getInstance(); 
     cal.setTime(df); 
     String theDate = String.format("%tY-%tm-%td", cal, cal, cal); 
     return theDate; 
    } 
+0

는'이 코드를 디버깅 좀 도와 주시겠습니까 도움이 될 것'* 우리가 디버깅이 코드를 *해야하는 이유? – Veluria

+0

왜 'SimpleDateFormat'대신에'String.format (..)'을 사용하고 있습니까? –

+0

DATE_FORMAT이 (가) 무엇입니까? –

답변

1

이 코드를 사용해보십시오이

if (this.subject != null && !this.subject.isEmpty()) { 
    DateFormat userDateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
    DateFormat outDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 

    Date df = userDateFormat.parsethis.subject); 
    return outDateFormat.format(df); 
} 
1

GregorianCalendar (필요하지 않음). Juste이를 사용

if (this.subject != null && !this.subject.isEmpty()) { 
    DateFormat userDateFormat = new SimpleDateFormat("dd-MM-yyyy"); 
    return userDateFormat.parse(this.subject); 
} 
관련 문제