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"
}
}