Me pregunto por qué 'Y' regresa 2012 mientras que 'y' regresa 2011 en SimpleDateFormat:
System.out.println(new SimpleDateFormat("Y").format(new Date())); // prints 2012
System.out.println(new SimpleDateFormat("y").format(new Date())); // prints 2011
¿Alguien puede explicar por qué?
java
date
simpledateformat
Ing.Fouad
fuente
fuente

Respuestas:
semana año y año. De javadoc
fuente
$ date Wed Dec 30 00:42:51 UTC 2015$ date +%G 2015$ date +%Y 2015Algún software se confunde:strftimecalcula hoy (29/12/2015) como si tuviera la semana 53 y la semana-año como 2015.Aquí hay una actualización de Java 8 con algo de código, ya que GregorianCalendar probablemente quedará obsoleto o se eliminará de futuras versiones de JDK.
El nuevo código se maneja en la
WeekFieldsclase, y específicamente para minúsculasy/ mayúsculasYcon el descriptor deweekBasedYear()acceso de campo.La configuración de esta
WeekFieldsinstancia depende de la ubicación y puede tener diferentes configuraciones dependiendo de ella, los países de EE. UU. Y Europa como Francia pueden tener un día diferente como comienzo de la semana.Por ejemplo,
DateFormatterBuilderde Java 8, cree una instancia del analizador con la configuración regional y use esta configuración regional para elYsímbolo:public final class DateTimeFormatterBuilder { ... private void parsePattern(String pattern) { ... } else if (cur == 'Y') { // Fields defined by Locale appendInternal(new WeekBasedFieldPrinterParser(cur, count)); } else { ... static final class WeekBasedFieldPrinterParser implements DateTimePrinterParser { ... /** * Gets the printerParser to use based on the field and the locale. * * @param locale the locale to use, not null * @return the formatter, not null * @throws IllegalArgumentException if the formatter cannot be found */ private DateTimePrinterParser printerParser(Locale locale) { WeekFields weekDef = WeekFields.of(locale); TemporalField field = null; switch (chr) { case 'Y': field = weekDef.weekBasedYear(); if (count == 2) { return new ReducedPrinterParser(field, 2, 2, 0, ReducedPrinterParser.BASE_DATE, 0); } else { return new NumberPrinterParser(field, count, 19, (count < 4) ? SignStyle.NORMAL : SignStyle.EXCEEDS_PAD, -1); } case 'e': case 'c': field = weekDef.dayOfWeek(); break; case 'w': field = weekDef.weekOfWeekBasedYear(); break; case 'W': field = weekDef.weekOfMonth(); break; default: throw new IllegalStateException("unreachable"); } return new NumberPrinterParser(field, (count == 2 ? 2 : 1), 2, SignStyle.NOT_NEGATIVE); } ... } ... }Aquí hay un ejemplo
System.out.format("Conundrum : %s%n", ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")) .format(DateTimeFormatter.ofPattern("YYYYMMdd'T'HHmms'S'"))); System.out.format("Solution : %s%n", ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")) .format(DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmms'S'"))); System.out.format("JVM Locale first day of week : %s%n", WeekFields.of(Locale.getDefault()).getFirstDayOfWeek()); System.out.format("US first day of week : %s%n", WeekFields.of(Locale.US).getFirstDayOfWeek()); System.out.format("France first day of week : %s%n", WeekFields.of(Locale.FRANCE).getFirstDayOfWeek()); System.out.format("JVM Locale min days in 1st week : %s%n", WeekFields.of(Locale.getDefault()).getMinimalDaysInFirstWeek()); System.out.format("US min days in 1st week : %s%n", WeekFields.of(Locale.US).getMinimalDaysInFirstWeek()); System.out.format("JVM Locale min days in 1st week : %s%n", WeekFields.of(Locale.FRANCE).getMinimalDaysInFirstWeek()); System.out.format("JVM Locale week based year (big Y): %s%n", ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")).get(WeekFields.of(Locale.FRANCE).weekBasedYear())); System.out.format("France week based year (big Y) : %s%n", ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")).get(WeekFields.of(Locale.FRANCE).weekBasedYear())); System.out.format("US week based year (big Y) : %s%n", ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")).get(WeekFields.of(Locale.US).weekBasedYear()));Y en lo que se refiere a la configuración regional y la carcasa superior
Y, se puede jugar ya sea con la opción de línea de comandos-Duser.language=(fr,en,es, etc.), o forzar la configuración regional en tiempo de invocación:System.out.format("English localized : %s%n", ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")) .format(DateTimeFormatter.ofPattern("YYYYMMdd'T'HHmms'S'", Locale.ENGLISH))); System.out.format("French localized : %s%n", ZonedDateTime.of(2015, 12, 30, 0, 0, 0, 0, ZoneId.of("UTC")) .format(DateTimeFormatter.ofPattern("YYYYMMdd'T'HHmms'S'", Locale.FRENCH)));fuente
Formato
Ypara obtener el año de la semana si el calendario admite el año de la semana. (getCalendar().isWeekDateSupported())fuente
He aprendido de la manera difícil la biblioteca de etiquetas JSTL
format:dateconshortque el formato solicitado utiliza AAAA bajo las sábanas. Lo que de hecho puede adelantar la fecha impresa un año.fuente
Convierto una fecha de un lado a otro: esperaría el mismo año cuando haga esto.
¡Fíjate cómo avanza uno!
Esto es malo: ¡YYYY!
Puedes ejecutarlo aquí .
import java.util.Date; import java.text.SimpleDateFormat; import java.text.ParseException; import static java.lang.System.out; class Playground { public static Date convertYYYYMMDDStr(String s) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date result = null; try { result = sdf.parse(s); } catch(ParseException e) { e.printStackTrace(); } return result; } public static String formatDateToStrWithSDF(Date d, SimpleDateFormat s) { return s.format(d); } public static void main(String[ ] args) { // DON'T DO. Use yyyy instead of YYYY SimpleDateFormat sdfdmy = new SimpleDateFormat("dd-MM-YYYY"); String jan1st2020sb = "2020-01-01"; Date jan1st2020d = convertYYYYMMDDStr(jan1st2020sb); String jan1st2020sa = formatDateToStrWithSDF(jan1st2020d, sdfdmy); out.println(jan1st2020sb); out.println(jan1st2020d); out.println(jan1st2020sa); String dec31st2020sb = "2020-12-31"; Date dec31st2020d = convertYYYYMMDDStr(dec31st2020sb); String dec31st2020sa = formatDateToStrWithSDF(dec31st2020d, sdfdmy); out.println(dec31st2020sb); out.println(dec31st2020d); out.println(dec31st2020sa); } }Esto es bueno: aaaa
fuente