Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
Periodo de Java Fechas de conversión para flotar

Tengo el día de nacimiento y el segundo LocalDate.now() Uso Periof para imprimir esto

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

pero si quiero imprimir este error "P5Y8M11D"

que puedo hacer para conseguirlo- "Vives en el mundo 10,23 años"

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Period#getYears

P5Y8M11D es el formato predeterminado de Period#toString . Puede obtener los componentes del período, por ejemplo, años, meses, días, etc. del objeto del Period según sus requisitos utilizando sus métodos de acceso correspondientes.

Manifestación:

 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); } }

Producción:

 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 Denunciar

0

Puede usar el formato de época para obtener segundos y compararlos:

 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda