Guest User

Untitled

a guest
Oct 21st, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. final LocalDate today = LocalDate.of(2015, 11, 20);
  2. final LocalDate nextSunday = today.with(next(SUNDAY));
  3. final LocalDate thisPastSunday = today.with(previous(SUNDAY));
  4.  
  5. import static java.time.DayOfWeek.SUNDAY;
  6. import static java.time.temporal.TemporalAdjusters.next;
  7. import static java.time.temporal.TemporalAdjusters.previous;
  8.  
  9. Calendar c=Calendar.getInstance();
  10. c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
  11. c.set(Calendar.HOUR_OF_DAY,0);
  12. c.set(Calendar.MINUTE,0);
  13. c.set(Calendar.SECOND,0);
  14. DateFormat df=new SimpleDateFormat("EEE yyyy/MM/dd HH:mm:ss");
  15. System.out.println(df.format(c.getTime())); // This past Sunday [ May include today ]
  16. c.add(Calendar.DATE,7);
  17. System.out.println(df.format(c.getTime())); // Next Sunday
  18. c.add(Calendar.DATE,7);
  19. System.out.println(df.format(c.getTime())); // Sunday after next
  20.  
  21. Sun 2010/12/26 00:00:00
  22. Sun 2011/01/02 00:00:00
  23. Sun 2011/01/09 00:00:00
  24.  
  25. DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
  26. Calendar now = new GregorianCalendar();
  27. Calendar start = new GregorianCalendar(now.get(Calendar.YEAR),
  28. now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH) );
  29.  
  30. while (start.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
  31. start.add(Calendar.DAY_OF_WEEK, -1);
  32. }
  33.  
  34. Calendar end = (Calendar) start.clone();
  35. end.add(Calendar.DAY_OF_MONTH, 7);
  36.  
  37. System.out.println(df.format(now.getTime()) );
  38. System.out.println(df.format(start.getTime()) );
  39. System.out.println(df.format(end.getTime()) );
  40.  
  41. // Get the date today, and then select midnight of the first day of the week
  42. // Joda uses ISO weeks, so all weeks start on Monday.
  43. // If you want to change the time zone, pass a DateTimeZone to the method toDateTimeAtStartOfDay()
  44. DateTime midnightToday = new LocalDate().toDateTimeAtStartOfDay();
  45. DateTime midnightMonday = midnightToday.withDayOfWeek( DateTimeConstants.MONDAY );
  46.  
  47. // If your week starts on Sunday, you need to subtract one. Adjust accordingly.
  48. DateTime midnightSunday = midnightMonday.plusDays( -1 );
  49.  
  50. DateTime midnightNextSunday = midnightSunday.plusDays( 7 );
  51. DateTime midnightSundayAfter = midnightNextSunday.plusDays( 7 );
  52.  
  53. Interval thisWeek = new Interval( midnightSunday, midnightNextSunday );
  54. Interval nextWeek = new Interval( midnightNextSunday, midnightSundayAfter );
  55.  
  56. if ( thisWeek.contains( someDate.getTime() )) System.out.println("This week");
  57. if ( nextWeek.contains( someDate.getTime() )) System.out.println("Next week");
  58.  
  59. Date today = new Date(2014, 7, 1); // assume today is 2014-07-01
  60. Date previousSunday = today.previousOrSame(DayOfWeek.SUNDAY); // 2014-06-29
  61. Date nextSunday = today.next(DayOfWeek.SUNDAY); // 2014-07-06
  62.  
  63. @Test
  64. public void testThisAndNextWeek() throws Exception {
  65. GregorianCalendar lastWeekCal = new GregorianCalendar(2010,
  66. Calendar.DECEMBER, 26);
  67. int lastWeek = lastWeekCal.get(Calendar.WEEK_OF_YEAR);
  68.  
  69. GregorianCalendar nextWeekCal = new GregorianCalendar(2011,
  70. Calendar.JANUARY, 4);
  71. int nextWeek = nextWeekCal.get(Calendar.WEEK_OF_YEAR);
  72.  
  73. GregorianCalendar todayCal = new GregorianCalendar(2010,
  74. Calendar.DECEMBER, 27);
  75. int currentWeek = todayCal.get(Calendar.WEEK_OF_YEAR);
  76.  
  77. assertTrue(lastWeekCal.before(todayCal));
  78. assertTrue(nextWeekCal.after(todayCal));
  79. assertEquals(51, lastWeek);
  80. assertEquals(52, currentWeek);
  81. // New Year.. so it's 1
  82. assertEquals(1, nextWeek);
  83. }
  84.  
  85. LocalDate date = ...;
  86. LocalDate newWeekDate = date.plusDays(1);
  87. while (newWeekDate.getDayOfWeek() != DayOfWeek.SATURDAY &&
  88. newWeekDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
  89. newWeekDate = date.plusDays(1);
  90. }
Add Comment
Please, Sign In to add comment