Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final LocalDate today = LocalDate.of(2015, 11, 20);
- final LocalDate nextSunday = today.with(next(SUNDAY));
- final LocalDate thisPastSunday = today.with(previous(SUNDAY));
- import static java.time.DayOfWeek.SUNDAY;
- import static java.time.temporal.TemporalAdjusters.next;
- import static java.time.temporal.TemporalAdjusters.previous;
- Calendar c=Calendar.getInstance();
- c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
- c.set(Calendar.HOUR_OF_DAY,0);
- c.set(Calendar.MINUTE,0);
- c.set(Calendar.SECOND,0);
- DateFormat df=new SimpleDateFormat("EEE yyyy/MM/dd HH:mm:ss");
- System.out.println(df.format(c.getTime())); // This past Sunday [ May include today ]
- c.add(Calendar.DATE,7);
- System.out.println(df.format(c.getTime())); // Next Sunday
- c.add(Calendar.DATE,7);
- System.out.println(df.format(c.getTime())); // Sunday after next
- Sun 2010/12/26 00:00:00
- Sun 2011/01/02 00:00:00
- Sun 2011/01/09 00:00:00
- DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
- Calendar now = new GregorianCalendar();
- Calendar start = new GregorianCalendar(now.get(Calendar.YEAR),
- now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH) );
- while (start.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
- start.add(Calendar.DAY_OF_WEEK, -1);
- }
- Calendar end = (Calendar) start.clone();
- end.add(Calendar.DAY_OF_MONTH, 7);
- System.out.println(df.format(now.getTime()) );
- System.out.println(df.format(start.getTime()) );
- System.out.println(df.format(end.getTime()) );
- // Get the date today, and then select midnight of the first day of the week
- // Joda uses ISO weeks, so all weeks start on Monday.
- // If you want to change the time zone, pass a DateTimeZone to the method toDateTimeAtStartOfDay()
- DateTime midnightToday = new LocalDate().toDateTimeAtStartOfDay();
- DateTime midnightMonday = midnightToday.withDayOfWeek( DateTimeConstants.MONDAY );
- // If your week starts on Sunday, you need to subtract one. Adjust accordingly.
- DateTime midnightSunday = midnightMonday.plusDays( -1 );
- DateTime midnightNextSunday = midnightSunday.plusDays( 7 );
- DateTime midnightSundayAfter = midnightNextSunday.plusDays( 7 );
- Interval thisWeek = new Interval( midnightSunday, midnightNextSunday );
- Interval nextWeek = new Interval( midnightNextSunday, midnightSundayAfter );
- if ( thisWeek.contains( someDate.getTime() )) System.out.println("This week");
- if ( nextWeek.contains( someDate.getTime() )) System.out.println("Next week");
- Date today = new Date(2014, 7, 1); // assume today is 2014-07-01
- Date previousSunday = today.previousOrSame(DayOfWeek.SUNDAY); // 2014-06-29
- Date nextSunday = today.next(DayOfWeek.SUNDAY); // 2014-07-06
- @Test
- public void testThisAndNextWeek() throws Exception {
- GregorianCalendar lastWeekCal = new GregorianCalendar(2010,
- Calendar.DECEMBER, 26);
- int lastWeek = lastWeekCal.get(Calendar.WEEK_OF_YEAR);
- GregorianCalendar nextWeekCal = new GregorianCalendar(2011,
- Calendar.JANUARY, 4);
- int nextWeek = nextWeekCal.get(Calendar.WEEK_OF_YEAR);
- GregorianCalendar todayCal = new GregorianCalendar(2010,
- Calendar.DECEMBER, 27);
- int currentWeek = todayCal.get(Calendar.WEEK_OF_YEAR);
- assertTrue(lastWeekCal.before(todayCal));
- assertTrue(nextWeekCal.after(todayCal));
- assertEquals(51, lastWeek);
- assertEquals(52, currentWeek);
- // New Year.. so it's 1
- assertEquals(1, nextWeek);
- }
- LocalDate date = ...;
- LocalDate newWeekDate = date.plusDays(1);
- while (newWeekDate.getDayOfWeek() != DayOfWeek.SATURDAY &&
- newWeekDate.getDayOfWeek() != DayOfWeek.SUNDAY) {
- newWeekDate = date.plusDays(1);
- }
Add Comment
Please, Sign In to add comment