Hola tengo una fecha creada con LocalDate y quiero cambiar su formato por este ejemplo
"Viernes, 4 de Julio de 2004". :silbar:
https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html ?
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
private static final String PATTERN = "eeee, d 'de' MMMM 'de' uuuu";
private String getFecha() {
return DateTimeFormater.ofPattern(PATTERN)
.withZone(ZoneId.systemDefault())
.withLocale(new Locale("es","AR"))
.format(LocalDate.now());
}
Output
martes, 23 de abril de 2019
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:
package aplicacion.test;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.Scanner;
/**
*
* @author Ana Kéldysh
*/
//Clase Principal que contiene el método main
public class Principal {
//Declaración del método main
public static void main(String[] args) {
Scanner entrada = new Scanner(System.in);
SimpleDateFormat formato = new SimpleDateFormat("EEEEEEEEE',' dd 'de' MMMMM 'de' yyyy");
int dia, mes, año;
System.out.println("Introduza fecha dd/MM/yyy: ");
dia=entrada.nextInt();
mes=entrada.nextInt();
año=entrada.nextInt();
LocalDate fecha = LocalDate.of(año, mes, dia);
System.out.println(fecha);
LocalDate fecha2 = fecha.plusDays(100);
System.out.println(formato.format(new Date()));
}
}
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
private static final Map<Long, String> DIAS = new HashMap<>();
private static final Map<Long, String> MESES = new HashMap<>();
private static final Scanner LEER = new Scanner(System.in);
//Constructor
public FormatearFecha() {
fillMap();
fecha();
}
private void fillMap() {
DIAS.put(1L, "Lunes");
DIAS.put(2L, "Martes");
DIAS.put(3L, "Miércoles");
DIAS.put(4L, "Jueves");
DIAS.put(5L, "Viernes");
DIAS.put(6L, "Sábado");
DIAS.put(7L, "Domingo");
MESES.put(1L, "Enero");
MESES.put(2L, "Febrero");
MESES.put(3L, "Marzo");
MESES.put(4L, "Abril");
MESES.put(5L, "Mayo");
MESES.put(6L, "Junio");
MESES.put(7L, "Julio");
MESES.put(8L, "Agosto");
MESES.put(9L, "Septiembre");
MESES.put(10L, "Octubre");
MESES.put(11L, "Noviembre");
MESES.put(12L, "Diciembre");
}
/**
* dd/MM/yyyy
* @return
*/
public void fecha() {
System.out.println("Introduce fecha, tipo dd/MM/yyyy");
System.out.println("Introduce dia ");
final int dia = LEER.nextInt();
System.out.println("Introduce Mes ");
final int mes = LEER.nextInt();
System.out.println("Introduce Año ");
final int año = LEER.nextInt();
LocalDate fecha = LocalDate.of(año, mes, dia);
System.out.println(format(fecha));
}
}
}
/**
* Ejemplo de formato de salida, Miercoles, 24 de Abril de 2019
* @return
*/
public String format(final Temporal temporal) {
return new DateTimeFormatterBuilder()
.appendText(ChronoField.DAY_OF_WEEK, DIAS )
.appendPattern(", d ")
.appendLiteral("de ")
.appendText(ChronoField.MONTH_OF_YEAR, MESES )
.appendLiteral(" de ")
.appendPattern("uuuu ")
.toFormatter(new Locale("es","AR"))
.withZone(ZoneId.systemDefault())
.format(temporal);
}