2012-02-15 6 views
2

날짜가 있습니다. 예를 들어 02/16/2012입니다. 데이터베이스 형식으로 작업하기 위해해야 ​​할 일은 2012-02-16로 변환하는 것입니다. 나는 이것이 매우 간단 할 것이라고 생각했다. 그러나 나는 그것을 작동시킬 수 없다. 여기에 내 데이트 파서가있다.DateFormat 구문 분석 오류

String endDateString = "02/16/2012" 
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd"); 
endDate = dateFormat.parse(endDateString) 

의견이 있으십니까? 감사.

답변

1

을 당신은 올바른 FROM 구문 분석한다 형식으로 변환 한 다음 다른 올바른 형식으로 TO 문자열로 변환하십시오. 뭐하시는 겁니까? yyyy-MM-dd 형식의 MM/dd/yyyy 형식으로 날짜를 해석하려고합니다. 즉 부정확합니다.

SAMPLE은

public static void main(String[] args) throws ParseException { 

    SimpleDateFormat sourceFormat = new SimpleDateFormat("MM/dd/yyyy"); 
    SimpleDateFormat destinationFormat = new SimpleDateFormat("yyyy-MM-dd"); 

    String sourceDateString = "02/16/2012"; 
    String destinationDateString; 

    Date dat; // this object stores "perfect" date object 

    // interpreting string with input format and converting it to date 
    dat = sourceFormat.parse(sourceDateString); 

    // expressing date object as string in destination format 
    destinationDateString = destinationFormat.format(dat); 

    System.out.format("Done from %s to %s\n", sourceDateString, destinationDateString)); 


} 
-1

당신은 잘못된 형식으로 파싱

new SimpleDateFormat("MM/dd/yyyy"); 
0
지정하는 날짜 포맷은 다음처럼 입력의 날짜 형식과 일치하는

:

SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
4

당신이 원하는 당신이 원하는

final SimpleDateFormat idf = new SimpleDateFormat("MM/dd/yyyy"); 
final Date indate = idf.parse("02/16/2012"); 

는 다음 형식으로 Date 객체를 재 포맷 Date 객체에 들어오는 형식을 구문 분석이다

final SimpleDateFormat odf = new SimpleDateFormat("yyyy-MM-dd"); 
final String s = odf.format(indate); 
0

이 스 니펫은 완료하는 방법을 알려줍니다.

SimpleDateFormat inputDateFormat = new SimpleDateFormat("MM/dd/yyyy"); 
SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
String endDateString = "02/16/2012"; 
String endDate =  outputDateFormat.format(inputDateFormat.parse(endDateString)); 

참조 : 또한 데이터베이스 수준에서 날짜를 변환 할 수 있습니다 SimpleDateFormat

0

. 예를 들어 MySQL에는 STR_TO_DATE 함수가 있습니다. 귀하의 경우 STR_TO_DATE ('02/16/2012 ','% m/% d/% Y ')가됩니다