1

Simple test case below is giving results different than expected.

import javax.xml.datatype.XMLGregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] args) {

            XMLGregorianCalendar xmlDate = new XMLGregorianCalendarImpl();
            xmlDate.setMonth(12);
            xmlDate.setDay(31);
            xmlDate.setYear(2019);
            xmlDate.setHour(0);
            xmlDate.setMinute(0);
            xmlDate.setSecond(0);
            Calendar calendar = xmlDate.toGregorianCalendar();
            SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
            Date dt = calendar.getTime();
           String ds1 = dt.toString();
           System.out.println("ds1 = " + ds1);
           String dateString = formatter.format(calendar.getTime());
           System.out.println("dateString = " + dateString );

    }
}

I cannot figure out why the year component of dateString is showing as 2020 instead of 2019.

ds1 = Tue Dec 31 00:00:00 EST 2019
dateString = 2020-12-31 00:00:00
OldProgrammer
  • 12,050
  • 4
  • 24
  • 45
  • 1
    FYI, the terribly flawed date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), and `GregorianCalendar` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/tutorial/datetime/TOC.html) classes built into Java 8 and later. – Basil Bourque Dec 23 '19 at 22:06
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Also `XMLGregorianCalendar` is old, and you may consider if a modern class isn’t a better alternative. – Ole V.V. Dec 24 '19 at 08:40

1 Answers1

3

Please change your code to

import javax.xml.datatype.XMLGregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] args) {

            XMLGregorianCalendar xmlDate = new XMLGregorianCalendarImpl();
            xmlDate.setMonth(12);
            xmlDate.setDay(31);
            xmlDate.setYear(2019);
            xmlDate.setHour(0);
            xmlDate.setMinute(0);
            xmlDate.setSecond(0);
            Calendar calendar = xmlDate.toGregorianCalendar();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dt = calendar.getTime();
           String ds1 = dt.toString();
           System.out.println("ds1 = " + ds1);
           String dateString = formatter.format(calendar.getTime());
           System.out.println("dateString = " + dateString );

    }
}

As YYYY represents year of the week and yyyy represents calendar year in simple date format.

More explanation here: Java SimpleDateFormat shifts Date by one year

Brooklyn99
  • 987
  • 13
  • 24