Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SimpleDataFormat将自定义日期格式转换为其他格式时出错_Java_Date_Date Format_Simpledateformat - Fatal编程技术网

Java 使用SimpleDataFormat将自定义日期格式转换为其他格式时出错

Java 使用SimpleDataFormat将自定义日期格式转换为其他格式时出错,java,date,date-format,simpledateformat,Java,Date,Date Format,Simpledateformat,我下面的代码有什么问题 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(dataForm

我下面的代码有什么问题

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);
}

没什么。这在我的电脑上很好用

编辑:那没用。您可能需要考虑特定的区域设置。如果您的区域设置需要不同的月名/日名,则会出现异常

编辑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();
    }

没什么。这在我的电脑上很好用

编辑:那没用。您可能需要考虑特定的区域设置。如果您的区域设置需要不同的月名/日名,则会出现异常

编辑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();
    }
这应该起作用:

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);
}
这应该起作用:

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);
}

你告诉我们出了什么事:发生了什么事?你告诉我们出了什么事:发生了什么事?