将字符串日期转换为不同格式的字符串日期

23

我是Java新手。Postgres数据库中包含日期格式为yyyy-MM-dd。我需要将其转换为dd-MM-yyyy

我尝试了这个,但显示的结果是错误的。

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

    String strDate = "2013-02-21";
      DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      Date da = (Date)formatter.parse(strDate);
      System.out.println("==Date is ==" + da);
      String strDateTime = formatter.format(da);

      System.out.println("==String date is : " + strDateTime);
}

你得到了什么输出? - Apurv
在日期对象上应用日期格式,而不是在字符串strDate上应用。这样应该可以正常工作。 - Priyank Thakkar
8个回答

60
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("2013-02-21");
System.out.println(format2.format(date));

我建议将 prevDateFormat 和 newDateFormat 命名。 - SilverTech

11

您需要使用两个DateFormat实例。一个包含输入字符串的格式,另一个包含输出字符串所需的格式。

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

    String strDate = "2013-02-21";

    DateFormat inputFormatter = new SimpleDateFormat("yyyy-MM-dd");
    Date da = (Date)inputFormatter.parse(strDate);
    System.out.println("==Date is ==" + da);

    DateFormat outputFormatter = new SimpleDateFormat("dd-MM-yyyy");
    String strDateTime = outputFormatter.format(da);
    System.out.println("==String date is : " + strDateTime);
}

5

请参考这些格式:Java日期格式文档

日期时间格式

试试这段代码:

String myDate= "2013-02-21";
DateFormat iFormatter = new SimpleDateFormat("yyyy-MM-dd");
DateFormat oFormatter = new SimpleDateFormat("dd-MM-yyyy");
String strDateTime = oFormatter.format(iFormatter.parse(myDate));

3
您需要一个格式来解析当前状态,以及一个格式来生成所需输出。
String strDate  = "2013-02-21";
DateFormat to   = new SimpleDateFormat("dd-MM-yyyy"); // wanted format
DateFormat from = new SimpleDateFormat("yyyy-MM-dd"); // current format
System.out.println(to.format(from.parse(strDate)));

2
尝试使用以下实用函数。
// convert String date to another string date
public String convertStringDateToAnotherStringDate(String stringdate, String stringdateformat, String returndateformat){        

        try {
            Date date = new SimpleDateFormat(stringdateformat).parse(stringdate);
            String returndate = new SimpleDateFormat(returndateformat).format(date);
            return returndate;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }

}

// call function with required parameter arguments
String resultDate = convertStringDateToAnotherStringDate("2017-06-22", "yyyy-MM-dd", "dd-MM-yyyy")
System.out.println(resultDate);

结果:2017年6月22日


0

首先,您不应该从Postgres数据库中将日期作为String获取。您应该将其作为表示日期的类型的对象获取。

其次,在2013年,使用DateSimpleDateFormat是常见和合理的,但现在已经过时了,并且新的更好的日期和时间类已经出现并得到普遍使用。在我看来,没有人应该再使用旧的类,我认为它们已经过时了。

要从数据库的ResultSet中获取LocalDate对象:

    LocalDate da = yourResultSet.getObject(3, LocalDate.class);

(我假设日期在查询的第三列中。)

将其格式化为 dd-MM-yyyy

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
    String strDateTime = da.format(formatter);
    System.out.println("==String date is : " + strDateTime);

这将会输出如下结果

==String date is : 21-02-2013

最后,如果您有像问题中那样的字符串,并希望将其重新格式化为所需的格式:

    String strDate = "2013-02-21";      
    LocalDate da = LocalDate.parse(strDate);
    System.out.println("==Date is ==" + da);

这将会打印出来

==Date is ==2013-02-21

我正在利用字符串匹配现代日期和时间类所使用的ISO 8601标准日期格式这个事实。因此,在这种情况下,我们不需要显式格式化程序来解析,就像解析其他格式一样。

将其格式化为所需格式与以前完全相同。

旧类带来的问题示例

在我的计算机上,问题代码的输出为:

==Date is ==Tue Aug 06 00:00:00 CET 26
==String date is : 06-08-0026

你明显得到了错误的结果,而且你不知道哪里出了问题。问题在于你使用了用于新字符串“dd-MM-yyyy”的格式化程序来解析你的数据库日期字符串“2013-02-21”。因此,它被解析为公元2013年2月21日。二月份没有2013天?对于默认设置的SimpleDateFormat来说,这不是问题,它只会继续计算进入下一个月和年,并在五年半后的8月6日结束,但仍然非常早。

如果我们尝试使用现代类进行类似的操作:

    LocalDate.parse(strDate, formatter);

我们得到了java.time.format.DateTimeParseException: Text '2013-02-21' could not be parsed at index 2。这是因为格式化程序指定了两位数的日期,所以当解析两个数字后,输入字符串中还有更多数字时,就会报错并报告错误。我认为这种行为更正确、更有帮助。


0

您正在使用相同的格式化程序来处理不同的格式。

您需要提供两个格式化程序new SimpleDateFormat("dd-MM-yyyy");,用于将2013-02-21转换为日期对象和new SimpleDateFormat("dd-MM-yyyy");,用于将日期对象格式化为字符串21-02-2013


0
你需要使用 SimpleDateFormat 实例并传递你喜欢的日期格式
当前日期格式:yyyy-MM-dd 所需日期格式:dd-MM-yyyy 现在尝试以下操作。它将生成所需的日期格式模式。
public class App {
    public static void main(String[] args) {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
        Date date = null;
        try {
            date = format1.parse("2013-02-21");
            System.out.println(format1.format(date)); //current format: 2013-02-21
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(format2.format(date)); //the format that is needed : 21-02-2013
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接