-8

i have a common doubt for a long while. During date formation if the input date in the format like "2019/05/22 02:00:23" then with the help following line we can format the date,

String inputDate = "2019/05/22 02:00:23";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);

I can see both input format and the required format is same. Suppose if i change the input date like below, it is showing the java.text.ParseException: Unparseable date: exception.

String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);

Please suggest how to achieve this.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Pearl
  • 384
  • 1
  • 8
  • 15
  • 3
    Well you changed the format of the date without changing the pattern so what did you expect? Did you even consider trying `"dd.MM.yyyy"`as the pattern? – Joakim Danielson May 29 '19 at 12:47
  • What do you want to achieve? `31.12.9999` to be parsed aswell with the given `dateFormatter`? Please [edit] your question and clarify – Lino May 29 '19 at 12:48
  • How to achieve what? – Oleksandr Husiev May 29 '19 at 12:48
  • 1
    Don't use deprecated classes… Read about the `java.time` package and its classes, that will help a lot, but not if you try to parse a date-`String` with a format that just doesn't fit. – deHaar May 29 '19 at 12:50
  • but even though i have changed the date format as "yyyy.MM.dd", still i'm getting the same error. If the date pattern and the given date is in the same format then i should should use date formatter? – Pearl May 29 '19 at 12:59
  • 1
    If you use a pattern that takes care of hours, minutes and seconds and then try to parse a date (only) with it, this try will fail... You have to parse the second date-`String` with a date-only pattern like `"yyyy.MM.dd"`, **not** `"yyyy.MM.dd HH:mm:ss"`. – deHaar May 29 '19 at 13:06
  • So, from this question i understood, the separator which we are having the date like "/" doesn't have any control with date format pattern. I mean both should be same. Suppose if we want to make the date separator from "." to "/" then i need to handle it manually. – Pearl May 29 '19 at 14:33
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 30 '19 at 12:54
  • What do you want HH:mm:ss to be in the result? – Ole V.V. May 30 '19 at 12:57
  • `LocalDate.parse(inputDate, DateTimeFormatter.ofPattern("dd.MM.uuuu"))` yields a `LocalDate` of `9999-12-31`. Yes, the format pattern used for parsing needs to match the string you are trynig to parse. – Ole V.V. May 30 '19 at 13:03
  • Did you look at other similar questions and examples? There are *a lot.* It’s best to do that before asking yours. This one is not bad to start with: [Change date format in a Java string](https://stackoverflow.com/questions/4772425/change-date-format-in-a-java-string). Use your search engine for more. Only ignore the pages using `SimpleDateFormat`. As I said, that class is old and troublesome. [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). – Ole V.V. May 30 '19 at 13:12

1 Answers1

0

Please try below i mentioned date format code,I hope it will help you,

From this,

String inputDate = "31.12.9999";
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date toDate = dateFormatter.parse(inputDate);

To Change:

Date date = new SimpleDateFormat("dd.MM.yyyy").parse("31.12.9999");
String formattedDate = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date);
System.out.println("formattedDate  :: " + formattedDate); 

Thanks,.

Ajith Deivam
  • 736
  • 4
  • 11
  • 27