Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Java Period Conversion dates to float

I have day of birth and second LocalDate.now() I use Periof for print this

            Period test = Period.between(dateofbirth,LocalDate.now());

but if i want print this error "P5Y8M11D"

what can i do to get it- "You live in the world 10,23 years"

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Period#getYears

P5Y8M11D is the default format of Period#toString. You can get the components of period e.g. years, months, days etc. from the object of Period as per your requirement using its corresponding accessor methods.

Demo:

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate dateofbirth = LocalDate.of(1975, 7, 15);
        Period test = Period.between(dateofbirth, LocalDate.now());
        System.out.printf("You have lived in the world for %d years.%n", test.getYears());

        double years = test.getYears() + test.getMonths() / 12.0 + test.getDays() / 365.0;
        System.out.printf("You have lived in the world for %.2f years.%n", years);

        DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.GERMAN);
        format.applyPattern("#,##0.00");
        String formattedValue = format.format(years);
        System.out.printf("You have lived in the world for %s years.%n", formattedValue);
    }
}

Output:

You have lived in the world for 45 years.
You have lived in the world for 45.36 years.
You have lived in the world for 45,36 years.
over 4 years ago · Santiago Trujillo Relatório

0

You can use the epoch format to get secondes and compare them :

public static void main(String[] args) {
    LocalDate dateofbirth = LocalDate.of(1975, 7, 15);
    long duration = System.currentTimeMillis() / 1000 - dateofbirth.atStartOfDay().toEpochSecond(ZoneOffset.UTC);
    System.out.printf("You have lived in the world for %f years.%n", new Float(duration) / 3600 / 24 / 365);
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda