I am trying to convert string to date using date format and used the following code but its shows an error.
public static Date ConvertStringtodate(String Date) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date Teststart = dateFormat.parse(Date);
return Teststart;
}
public static void main(String[]agrs) throws ParseException {
System.out.println(ConvertStringtodate("2022.02.10 17:54:55"));
}
Here is the error
Exception in thread "main" java.lang.IllegalArgumentException: Cannot format given Object as a Date at java.text.DateFormat.format(DateFormat.java:310) at java.text.Format.format(Format.java:157)
hhin your format pattern should beHH- read the documentation forSimpleDateFormatclosely to see the difference.formatwhen you're actually callingparse- please could you include the full stack trace?java.text.DateFormat.parse. (I've just tried your exact code.) In future, please make the code you provide reflect the error you provide. As well as the answer you've received, I'd strongly advise: a) start following Java naming conventions; b) use java.time instead of the old java.util.Date and java.text.SimpleDateFormat classes.