Java의 SimpleDateFormat
을 사용하여 문자열을 적절한 날짜 형식으로 변환하려고합니다. 어떤 이유로 3 월, 5 월, 10 월, 12 월과 같은 특정 월에는 작동하지 않습니다. 누군가 나를 도울 수 있습니까? 다른 모든 달에는 정상적으로 작동합니다.문자열을 형식이 지정된 날짜로 변환
import java.sql.Date;
import java.text.SimpleDateFormat;
import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;
public class test {
public static void main(String args[]) throws java.text.ParseException {
try {
SimpleDateFormat parse = new SimpleDateFormat("dd. MMM yyyy hh:mm:ss");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//why this doesn't work with certain months like Mar, May, Oct, and Dec? otherwise it works fine
String dateTime = "01. Jun 2010 15:30:32";
//String dateTime = "07. Mar 2011 15:20:10";
//String dateTime = "07. May 2011 15:20:10";
//String dateTime = "07. Oct 2011 15:20:10";
//String dateTime = "07. Dec 2011 15:20:10";
java.util.Date parsed =parse.parse(dateTime);
System.out.println("formatted: " + formatter.format(parsed));
} catch(ParseException e) {
System.out.println("Caught " + e);
}
}
}
코드를 실행 해 보았는데 완벽하게 작동했습니다. 당신이 직면 한 문제는 무엇입니까? BTW, 당신은'com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException'가 아닌'java.text.ParseException'을 잡아야 만합니다. – Nishan
'com.sun. *'패키지를 IDE autosuggest list. – BalusC