Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. public static String getStartOfNextWeek(String DATE){
  2.  
  3. String format = "dd.MM.yyyy";SimpleDateFormat df = new SimpleDateFormat(format);
  4.  
  5. Date date = null;
  6. try {
  7. date = df.parse(DATE);
  8. } catch (ParseException e) {
  9. e.printStackTrace();
  10. }
  11.  
  12. Calendar cal = Calendar.getInstance();
  13.  
  14. cal.setTime(date);
  15. int week = cal.get(Calendar.WEEK_OF_YEAR);
  16. int year = cal.get(Calendar.YEAR);
  17.  
  18. Calendar calendar = new GregorianCalendar();
  19. calendar.clear();
  20. calendar.set(Calendar.YEAR, year);
  21. calendar.set(Calendar.WEEK_OF_YEAR, week);
  22.  
  23. //add 8 days to get next weeks Monday
  24.  
  25. calendar.add(Calendar.DAY_OF_WEEK, 8);
  26.  
  27. Date startDate = calendar.getTime();
  28.  
  29. SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy");
  30.  
  31. String start = df2.format(startDate);
  32.  
  33. return start;
  34.  
  35. input: 15.12.2014
  36. output: 22.12.2014 CORRECT
  37. input: 22.12.2014
  38. output: 29.12.2014 CORRECT
  39. input: 29.12.2014
  40. output: 6.1.2014 WRONG
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement