Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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日期_Java_Spring_Parsing_Date Format - Fatal编程技术网

如何格式化java日期

如何格式化java日期,java,spring,parsing,date-format,Java,Spring,Parsing,Date Format,我正在从事一个Spring项目,我必须按上传日期搜索文档。因此,当我将日期作为DAO层中某个方法的参数传递时,收到的结果如下:Thu Jun 06 00:03:49 WEST 2013。我想把它格式化为:2013-06-06 我已经使用了这段代码,但它返回06/06/13和DateFormat的其他常量,如DateFormat.MEDIUM。。。不要归还我等待的东西 DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT)

我正在从事一个Spring项目,我必须按上传日期搜索文档。因此,当我将日期作为DAO层中某个方法的参数传递时,收到的结果如下:Thu Jun 06 00:03:49 WEST 2013。我想把它格式化为:2013-06-06

我已经使用了这段代码,但它返回06/06/13和DateFormat的其他常量,如DateFormat.MEDIUM。。。不要归还我等待的东西

DateFormat shortDf = DateFormat.getDateInstance(DateFormat.SHORT);       
System.out.println(shortDf.format(new Date())); // return 06/06/13 it's short
我也尝试过SimpleDateFormat,如下所示:

public static Date parseDate(String date, String format)throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat(format,Locale.ENGLISH);
return formatter.parse(date);
}
但它仍会引发解析异常:

java.text.ParseException: Unparseable date: "Thu Jun 06 00:23:33 WEST 2013"
at java.text.DateFormat.parse(DateFormat.java:337)
at TestApp.main(TestApp.java:20)

这是我能得到的最接近的结果:

DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");

这是我能得到的最接近的结果:

DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");

如果您想将日期格式化为您自己的格式,如2013-06-06,SimpleDataFormatter是一种常见的解决方案。但代码中的错误在于,格式化日期的返回类型错误。下面是一个例子:

Date d=new Date();
String formattedDate=format(d);

System.out.println("This is your date: "+formattedDate);  

public String format(String date){
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  return sdf.format(date);
}  
要将日期格式化为您自己的格式,请使用sdf.format,而不是sdf.parse。 sdf.parse用于将字符串转换为日期,而sdf.format用于将日期转换为指定格式的字符串


sdf.parse返回日期,sdf.format返回字符串。

如果要将日期格式化为您自己的格式,如2013-06-06,SimpleDataFormatter是一种常见的解决方案。但代码中的错误在于,格式化日期的返回类型错误。下面是一个例子:

Date d=new Date();
String formattedDate=format(d);

System.out.println("This is your date: "+formattedDate);  

public String format(String date){
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  return sdf.format(date);
}  
要将日期格式化为您自己的格式,请使用sdf.format,而不是sdf.parse。 sdf.parse用于将字符串转换为日期,而sdf.format用于将日期转换为指定格式的字符串


sdf.parse返回日期,sdf.format返回字符串。

这应该适用于您的情况:

    DateFormat sourceFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
    DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");

    try {
        Date date = sourceFormat.parse("Thu Jun 06 00:23:33 WEST 2013");
        String formatted = targetFormat.format(date);
        System.out.println(formatted);
    } catch (ParseException e) {
        e.printStackTrace();
    }
首先,您需要使用正确的格式解析日期,并将locale.US更改为适合您的格式。您得到的异常是由不正确的分析格式或缺少区域设置引起的

EEE MMM dd HH:mm:ss zzz  yyyy
Thu Jun 06 00:23:33 WEST 2013
然后使用此格式化字符串格式化结果:

yyyy-MM-dd
2013-06-06

这应该适用于您的情况:

    DateFormat sourceFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
    DateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");

    try {
        Date date = sourceFormat.parse("Thu Jun 06 00:23:33 WEST 2013");
        String formatted = targetFormat.format(date);
        System.out.println(formatted);
    } catch (ParseException e) {
        e.printStackTrace();
    }
首先,您需要使用正确的格式解析日期,并将locale.US更改为适合您的格式。您得到的异常是由不正确的分析格式或缺少区域设置引起的

EEE MMM dd HH:mm:ss zzz  yyyy
Thu Jun 06 00:23:33 WEST 2013
然后使用此格式化字符串格式化结果:

yyyy-MM-dd
2013-06-06

请记住,SimpleDataFormat类不是线程安全的,如果使用不当,会在多线程环境中导致问题,这意味着您必须非常小心地在Spring中使用它,因为它是多线程环境

为什么它不是线程安全的

SimpleDataFormat类为格式化和解析日期而改变其内部状态。这就是为什么当多个线程同时使用同一个SimpleDataFormat实例时会出现问题

如何在多线程环境中使用SimpleDataFormat

有两种选择-

为每个线程创建SimpleDataFormat的新实例。 使用同步关键字或锁同步多个线程的并发访问。 请记住SimpleDataFormat类不是线程安全的,如果使用不当会在多线程环境中导致问题,这意味着您必须非常小心地在Spring中使用它,因为它是多线程环境

为什么它不是线程安全的

SimpleDataFormat类为格式化和解析日期而改变其内部状态。这就是为什么当多个线程同时使用同一个SimpleDataFormat实例时会出现问题

如何在多线程环境中使用SimpleDataFormat

有两种选择-

为每个线程创建SimpleDataFormat的新实例。 使用同步关键字或锁同步多个线程的并发访问。
+1此DateFormat将转换日期中的建议字符串,只需确保传递正确的区域设置即可识别日期和月份。+1此DateFormat将转换日期中的建议字符串,只需确保通过正确的区域设置来识别日期和月份。是的,我找到了与您描述的类似的解决方案。请将此线程标记为已回答,如果您觉得它回答了您的问题,所以人们知道在这条线索中找到了一个解决方案:是的,我找到了这个解决方案,就像你描述的一样。如果你觉得这条线索回答了你的问题,请将它标记为已回答,所以人们知道在这个线程中找到了一个解决方案:你是对的,亲爱的,我必须使用format方法,而不是pare-one-DateFormat-formatter=new-simpledateformatyyy-MM-dd;System.out.PrintlFormatted日期:+formatter.FormattCurrentDate;//返回2013/06/06,如等待感谢LOT您是对的,亲爱的,我必须使用format方法而不是pare one DateFormat formatter=new SimpleDataFormatyyy MM dd;System.out.PrintlFormatted日期:+formatter.FormattCurrentDate;//返回2013/06/06,如LOT或最佳选项:使用DateTimeFormatter from。或最佳选项:使用DateTimeFormatter from。