elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Arreglado, de nuevo, el registro del warzone (wargame) de EHN


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  fecha con LocalDate
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: fecha con LocalDate  (Leído 2,150 veces)
Beginner Web


Desconectado Desconectado

Mensajes: 634


youtu.be/0YhflLRE-DA


Ver Perfil
fecha con LocalDate
« en: 23 Abril 2019, 07:12 am »

Hola tengo una fecha creada con LocalDate y quiero cambiar su formato por este ejemplo
“Viernes, 4 de Julio de 2004”.  :silbar:


En línea

7w7
CalgaryCorpus


Desconectado Desconectado

Mensajes: 323


Ver Perfil WWW
Re: fecha con LocalDate
« Respuesta #1 en: 23 Abril 2019, 07:24 am »

https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html ?


En línea

Aqui mi perfil en LinkedIn, invitame un cafe aqui
rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: fecha con LocalDate
« Respuesta #2 en: 23 Abril 2019, 15:33 pm »

Chavalina que tal? Recuerdo que en un privado te mande algo parecido, no se si recuerdas, aquí te muestro pero en minúsculas, falta mostrar día y fecha en mayúsculas  :xD, no recuerdo, o procesar el String resultante, pero lo considero algo ineficiente

Código
  1. private static final String PATTERN = "eeee, d 'de' MMMM 'de' uuuu";

Código
  1. private String getFecha() {
  2.    return DateTimeFormater.ofPattern(PATTERN)
  3.                  .withZone(ZoneId.systemDefault())
  4.                  .withLocale(new Locale("es","AR"))
  5.                  .format(LocalDate.now());
  6. }

Output
Código
  1. martes, 23 de abril de 2019
« Última modificación: 23 Abril 2019, 16:05 pm por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
Beginner Web


Desconectado Desconectado

Mensajes: 634


youtu.be/0YhflLRE-DA


Ver Perfil
Re: fecha con LocalDate
« Respuesta #3 en: 23 Abril 2019, 16:11 pm »

Bueno, se ve complicado, hasta ahora tengo hecho esto, no me gusta estar usando distintos tipos de clases para fechas
Queria usar de tipo fecha LocalDate  y el formateador unicamente, logré esto y ... ahora veo que  hay un formateador para LocalDate  :huh:

Código
  1. package aplicacion.test;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.time.LocalDate;
  5. import java.util.Date;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  *
  10.  * @author Ana Kéldysh
  11.  */
  12. //Clase Principal que contiene el método main
  13. public class Principal {
  14.  
  15.    //Declaración del método main
  16.    public static void main(String[] args) {
  17.  
  18.        Scanner entrada = new Scanner(System.in);
  19.        SimpleDateFormat formato = new SimpleDateFormat("EEEEEEEEE',' dd 'de' MMMMM 'de' yyyy");
  20.  
  21.        int dia, mes, año;
  22.        System.out.println("Introduza fecha dd/MM/yyy: ");
  23.        dia=entrada.nextInt();
  24.        mes=entrada.nextInt();
  25.        año=entrada.nextInt();
  26.        LocalDate fecha = LocalDate.of(año, mes, dia);
  27.        System.out.println(fecha);
  28.        LocalDate fecha2 = fecha.plusDays(100);
  29.        System.out.println(formato.format(new Date()));        
  30.  
  31.    }
  32. }
« Última modificación: 23 Abril 2019, 16:26 pm por Beginner Web » En línea

7w7
rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: fecha con LocalDate
« Respuesta #4 en: 23 Abril 2019, 16:22 pm »

Vas bien, pero entonces de qué vale, ayudarte si dices que se ve complicado? Ya varias veces te he comentado que debes arriesgarte también XD, estoy desde el cell, luego lo formateo mejor.  :xD

Constantes


Código
  1. private static final Map<Long, String> DIAS = new HashMap<>();
  2. private static final Map<Long, String> MESES = new HashMap<>();
  3. private static final Scanner LEER = new Scanner(System.in);
  4.  


Código
  1.  //Constructor
  2.       public FormatearFecha() {
  3.                fillMap();
  4.                fecha();
  5.        }
  6.  
  7.        private void fillMap() {
  8.                 DIAS.put(1L, "Lunes");
  9.                 DIAS.put(2L, "Martes");
  10.                 DIAS.put(3L, "Miércoles");
  11.                 DIAS.put(4L, "Jueves");
  12.                 DIAS.put(5L, "Viernes");
  13.                 DIAS.put(6L, "Sábado");
  14.                 DIAS.put(7L, "Domingo");
  15.  
  16.                 MESES.put(1L, "Enero");
  17.                 MESES.put(2L, "Febrero");
  18.                 MESES.put(3L, "Marzo");
  19.                 MESES.put(4L, "Abril");
  20.                 MESES.put(5L, "Mayo");
  21.                 MESES.put(6L, "Junio");
  22.                 MESES.put(7L, "Julio");
  23.                 MESES.put(8L, "Agosto");
  24.                 MESES.put(9L, "Septiembre");
  25.                 MESES.put(10L, "Octubre");
  26.                 MESES.put(11L, "Noviembre");
  27.                 MESES.put(12L, "Diciembre");
  28.        }
  29.  
  30.        /**
  31.          *  dd/MM/yyyy
  32.          * @return
  33.          */
  34.        public void fecha() {
  35.                System.out.println("Introduce fecha, tipo dd/MM/yyyy");
  36.  
  37.                System.out.println("Introduce dia ");
  38.                final int dia = LEER.nextInt();
  39.  
  40.                System.out.println("Introduce Mes ");
  41.                final int mes = LEER.nextInt();
  42.  
  43.                System.out.println("Introduce Año ");
  44.                final int año = LEER.nextInt();
  45.  
  46.                LocalDate fecha  = LocalDate.of(año, mes, dia);
  47.  
  48.                System.out.println(format(fecha));
  49.  
  50.        }
  51.  
  52.        }
  53.        }
  54.         /**
  55.          * Ejemplo de formato de salida, Miercoles, 24 de Abril de 2019
  56.          * @return
  57.          */
  58.        public String format(final Temporal temporal) {
  59.                return new DateTimeFormatterBuilder()
  60.                                .appendText(ChronoField.DAY_OF_WEEK, DIAS )
  61.                                .appendPattern(", d ")
  62.                                .appendLiteral("de ")
  63.                                .appendText(ChronoField.MONTH_OF_YEAR, MESES )
  64.                                .appendLiteral(" de ")
  65.                                .appendPattern("uuuu ")
  66.                                .toFormatter(new Locale("es","AR"))
  67.                                .withZone(ZoneId.systemDefault())
  68.                                .format(temporal);
  69.   }
« Última modificación: 4 Mayo 2019, 16:38 pm por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines