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

Java 将字符串日期转换为不同格式的字符串日期,java,simpledateformat,date-conversion,Java,Simpledateformat,Date Conversion,我是Java新手。Postgres db contain date格式为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");

我是Java新手。Postgres db contain date格式为
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);
}

您需要使用两个
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);
}

您需要一种格式来解析当前状态,需要一种格式来生成所需的输出

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

您使用相同的格式化程序处理不同的格式

您需要提供两个格式化程序
newsimpledateformat(“dd-MM-yyyy”)用于将
2013-02-21
转换为日期对象和
新简化格式(“dd-MM-yyyy”)
用于将日期对象格式化为字符串
21-02-2013

请参考以下格式:

请尝试以下代码:

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

尝试以下实用程序功能

// 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日


首先,你不应该从Postgres数据库中以
字符串的形式获取日期。您应该将其作为表示日期的类型的对象获取

第二,虽然2013年,
Date
SimpleDateFormat
的使用非常普遍和合理,但日子一天天过去了,新的更好的日期和时间类已经出现并普遍使用。没有人应该再使用旧课了,我认为他们已经过时了。 要从数据库中的
ResultSet
获取
LocalDate
对象,请执行以下操作:

    LocalDate da = yourResultSet.getObject(3, LocalDate.class);
(我假设日期在查询的第3列中)

要将其格式化为
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
解析db日期字符串,
2013-02-21
。所以它被解析为公元2013年2月21日。2月份没有2013天吗?使用默认设置的
SimpleDateFormat
没有问题,它只是在接下来的月份和年份中继续计算天数,并在五年半后的8月6日结束,但在历史上仍然很早

如果我们尝试对现代课堂进行类似的操作:

    LocalDate.parse(strDate, formatter);

-我们得到
java.time.format.DateTimeParseException:无法在索引2处解析文本“2013-02-21”
。这是因为格式化程序指示的是两位数的日期,所以当解析两位数后,输入字符串中有更多的位数时,这是一个错误,并且会按此报告。我认为这个行为更正确和更有帮助。

< P>你需要使用<代码> SimpleDateFormat < /Cord>实例,并通过<强>日期模式< /强>你更喜欢.< 当前模式:
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
    }
}

您得到了什么输出?在日期对象上应用日期格式,而不是在字符串strDate上应用日期格式。它应该很好用,谢谢!我建议命名prevDateFormat和newDateFormat。
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
    }
}