-1

I am using Oracle java 1.7.0_45 and it appears that SimpleDateFormat.format method returns 2015 for the year component instead of 2014.

Here is the unit test:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("YYYYMMdd");

    public static void main(String[] args) {
        Date today = new Date();
        System.out.println("OS: " + System.getProperty("os.name") + " " + System.getProperty("os.version"));
        System.out.println("Java Vendor: " + System.getProperty("java.vendor"));
        System.out.println("Java Version: " + System.getProperty("java.version"));
        System.out.println("Today: " + today.toString() + " formatted: " + DATE_FORMAT.format(today));
    }
}

And here is the output from the unit test: OS: Mac OS X 10.10.1 Java Vendor: Oracle Corporation Java Version: 1.7.0_45 Today: Mon Dec 29 09:43:00 EST 2014 formatted: 20151229

sdc
  • 89
  • 2

3 Answers3

1

Try this instead:

private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");

yyyy and YYYY are different, as MM and mm are.

realUser404
  • 2,111
  • 3
  • 20
  • 38
1

pattern used - yyyyMMdd

import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");

    public static void main(String[] args) {
        Date today = new Date();
        System.out.println("Java Vendor: " + System.getProperty("java.vendor"));
        System.out.println("Java Version: " + System.getProperty("java.version"));
        System.out.println("Today: " + today.toString() + " formatted: " + DATE_FORMAT.format(today));
    }
}

output

Java Vendor: Sun Microsystems Inc.
Java Version: 1.6.0_31
Today: Mon Dec 29 20:21:17 IST 2014 formatted: 20141229
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
1

Use yyyyMMdd in stead. YYYY is for week Year (see javadoc) which might, depending on your Locale, sometimes return the 53rd week of the past year when using early jan dates or the 1st week of the new year when using late december dates.

joostschouten
  • 3,863
  • 1
  • 18
  • 31