1
public String parseDate(LocalDate localDate) {
        int year = localDate.getYear();
        int month = localDate.getMonthValue();
        int dayOfMonth = localDate.getDayOfMonth();
        return dayOfMonth + "-" + month + "-" + year;
    }
Вывод: 10-5-2020 
Нужно: 10-05-2020
1
  • String.format("%02d-%02d-%04d" , dayOfMonth,month,year) 7 сен 2020 в 10:46

1 ответ 1

1

Так как вы распарсили значения, то попробуйте так через формат

String.format("%02d-%02d-%04d" , dayOfMonth,month,year) 

String.format("%02d-%02d-%04d" , 1,1,2020); // 01-01-2020

А хорошо использовать форматер из LocalDate

localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")) 
2
  • Спасибо localDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")) - это то, что я искал.
    – Dmitriy
    7 сен 2020 в 11:29
  • @Dmitriy можете пометить как полезный ответ 7 сен 2020 в 11:31

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.