fork download
  1. import java.text.SimpleDateFormat;
  2. import java.util.Calendar;
  3. import java.util.Locale;
  4. import java.util.TimeZone;
  5.  
  6.  
  7. class MyTest {
  8.  
  9. private static Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("CET"), Locale.FRANCE);
  10. private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  11.  
  12. public static void main(String[] args) {
  13.  
  14. // Set to any date.
  15. calendar.set(2013, 10, 3);
  16. System.out.println(dateFormat.format(calendar.getTime()));
  17.  
  18. // Set to another day.
  19. calendar.set(2014, 0, 15);
  20. // --- THE WTF STARTS HERE ---
  21. // Uncommenting the line below returns the correct date in the end.
  22. //calendar.getTime();
  23.  
  24. // Set to monday of current week.
  25. calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
  26.  
  27. // Expected outdate is 20140113.
  28. System.out.println(dateFormat.format(calendar.getTime()));
  29.  
  30. }
  31.  
  32. }
  33.  
Success #stdin #stdout 0.1s 380928KB
stdin
Standard input is empty
stdout
20131103
20131223