Converting a Date String to a Date String A Different Format

I am new to Java. Postgres db contains the date format yyyy-MM-dd . I need to convert to dd-MM-yyyy .

I tried this, but the wrong result is display

  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); } 
+4
source share
7 answers
 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)); 
+13
source

You need to use two instances of DateFormat . One that contains the format of the input string and a second that contains the desired format for the output string.

 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); } 
+9
source

You need a format for parsing the current state and a format for creating the desired result.

 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
source

Link to these formats Java Date Format Documents :

Formats for datetime

Try this code:

 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)); 
+1
source
 // convertStringDateToAnotherStringDate 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); 

result: 06/22/2017

+1
source

you use the same formatter to handle different formats.

you need to provide two formatting new SimpleDateFormat("dd-MM-yyyy"); to convert the object 2013-02-21 to a date and new SimpleDateFormat("dd-MM-yyyy"); to form the date object in line 21-02-2013 .

0
source

First, you should not get your date as a String from your Postgres database. You should get it as an object of the type representing the date.

Secondly, while in 2013 the use of Date and SimpleDateFormat was commonplace and reasonable, the days passed and the new and better date and time classes came out and became generally accepted. IMHO no one else should use the old classes, and I consider them obsolete.

To get a LocalDate object from a ResultSet from your database:

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

(I assume the date is listed in column 3 in the request).

To format it in dd-MM-yyyy :

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

This will give a result, for example

 ==String date is : 21-02-2013 

Finally, if you have a line, as in the question, and you want to reformat it to the desired format:

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

Will print

 ==Date is ==2013-02-21 

I use the fact that the string complies with the ISO 8601 date standard, which is the default for modern date and time classes. Therefore, in this case, we do not need an explicit formatter for parsing, as well as for parsing other formats.

Formatting to the desired format is exactly the same as before.

An example of a problem with old classes

On my computer, the output from the code in the question

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

You obviously get the wrong result, and you don’t understand what happened. It wasn’t good that you parsed the db date string, 2013-02-21 , with the formatting you intended for the new string, dd-MM-yyyy . Thus, it is analyzed as 2013 February 21st. Are there days in February in February? There is no problem with SimpleDateFormat with default settings, it just keeps counting the days in the following months and years and ends on August 6 five and a half years later, but is still very early in history.

If we try to do similarly to modern classes:

  LocalDate.parse(strDate, formatter); 

- we get java.time.format.DateTimeParseException: Text '2013-02-21' could not be parsed at index 2 . This is due to the fact that the formatter indicated a 2-digit day, so when after parsing two digits there are more digits in the input line, an error appears and is reported as such. I consider this behavior more correct and useful.

0
source

All Articles