In my application I have some strings for show date such as this : 2023-1-5.
But I want convert this to 2023-01-05.
I know I can check this with if
condition and when under 10 add 0 to numbers!
But I want use best way for this and automatically, not with if condition.
How can I it?
CodePudding user response:
String date ="2023-1-5";
String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String dateNew = simpleDateFormat.format(date);
System.out.println(dateNew);