1

以下の私のコードの何が問題になっていますか?

try {

   // dataFormatOrigin (Wed Jun 01 14:12:42 2011)  
   // this is original string with the date information

   SimpleDateFormat sdfSource = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy");

   Date date = sdfSource.parse(dataFormatOrigin);

   // (01/06/2011 14:12:42) - the destination format that I want to have

   SimpleDateFormat sdfDestination = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");

   dataFormatDest = sdfDestination.format(date);

   System.out.println("Date is converted to MM-dd-yyyy hh:mm:ss");

   System.out.println("Converted date is : " + dataFormatDest);

} catch (ParseException pe) {
   System.out.println("Parse Exception : " + pe);
}
4

2 に答える 2

2

何もない。これは私のコンピューターでは問題なく動作します。

編集:それは役に立ちませんでした。考慮する必要のある特定のロケール設定がある場合があります。ロケールが異なる月名/曜日名を予期している場合は、例外が発生します。

編集2:これを試してください:

try{
        String dataFormatOrigin = "Wed Jun 01 14:12:42 2011";
        // this is original string with the date information 
        SimpleDateFormat sdfSource = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy", Locale.US);

        Date date = sdfSource.parse(dataFormatOrigin);

        // (01/06/2011 14:12:42) - the destination format that I want to have 
        SimpleDateFormat sdfDestination = new SimpleDateFormat( "dd-MM-yyyy hh:mm:ss");

        String dataFormatDest = sdfDestination.format(date);

        System.out .println("Date is converted to MM-dd-yyyy hh:mm:ss"); System.out .println("Converted date is : " + dataFormatDest);

    } catch (ParseException pe) { 
        System.out.println("Parse Exception : " + pe); 
        pe.printStackTrace();
    }
于 2011-06-02T23:29:56.390 に答える
2

これは機能するはずです:

try {

   // dataFormatOrigin (Wed Jun 01 14:12:42 2011)  
   // this is original string with the date information



   // (01/06/2011 14:12:42) - the destination format
   SimpleDateFormat sdfDestination = new SimpleDateFormat(
    "dd-MM-yyyy hh:mm:ss");

   sdfDestination.setLenient( true ); 
   // ^ Makes it not care about the format when parsing

   Date date = sdfDestination.parse(dataFormatOrigin);

   dataFormatDest = sdfDestination.format(date);

   System.out
     .println("Date is converted to MM-dd-yyyy hh:mm:ss");

   System.out
     .println("Converted date is : " + dataFormatDest);


} catch (ParseException pe) {
   System.out.println("Parse Exception : " + pe);
}
于 2011-06-03T00:02:36.140 に答える