0

When I try to format 20151227 to week by pattern yyyyww, I got result 201501 which really confused me. I thought it would be 201601.

Then I format 201601 to day by pattern yyyyMMdd, I got 20151227. So weird.

Here is the full code:

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

public class Test {

    public static String format(Date d, String format) {
        return new SimpleDateFormat(format).format(d);
    }

    public static Date parse(String t, String format) throws ParseException {
        return new SimpleDateFormat(format).parse(t);
    }


    public static void main(String[] args) throws ParseException {
        System.out.println(format(parse("20151227", "yyyyMMdd"), "yyyyww"));// The console prints "201501"

        System.out.println(format(parse("201601", "yyyyww"), "yyyyMMdd"));//prints "20151227"
    }
}
Thomas Orlita
  • 1,554
  • 14
  • 28
Joey
  • 1
  • 2
  • It formattes only long numbers – Roman C Feb 03 '16 at 13:14
  • 4
    Related: http://stackoverflow.com/q/23542216/1743880 and http://stackoverflow.com/q/6754024/1743880 – Tunaki Feb 03 '16 at 13:19
  • `LocalDate.parse( "20151227" , DateTimeFormatter.BASIC_ISO_DATE )`. For weeks, use standard [ISO 8601 week](https://en.wikipedia.org/wiki/ISO_week_date) and the [`YearWeek`](https://www.threeten.org/threeten-extra/apidocs/org.threeten.extra/org/threeten/extra/YearWeek.html) class from [*ThreeTen-Extra*](https://www.threeten.org/threeten-extra/) library. Never use `Date` or `SimpleDateFormat` classes, outmoded by JSR 310. – Basil Bourque Sep 01 '19 at 17:53

0 Answers0