Jodah Time: Last Week and Month

What would be the best way to get the start and end dates of the most recent full week and last completed month based on the date.

IE, given today's date, how to find out the week that has just ended is the same for a month.

+2
source share
3 answers

I got a solution

LocalDate today = new LocalDate() LocalDate lastWeek = today.minusWeeks(1) start= lastWeek .dayOfWeek().withMinimumValue().minusDays(1) end = lastWeek.dayOfWeek().withMaximumValue().minusDays(1) LocalDate lastMonth = today.minusMonths(1) timeStart = lastMonth.dayOfMonth().withMinimumValue() timeEnd = lastMonth.dayOfMonth().withMaximumValue() 
+5
source

Within a month, this is easy - something like:

 LocalDate endOfPreviousMonth = date.withDayOfMonth(1).minusDays(1); 

This is a bit more difficult during the week. You can do it:

 LocalDate previousSunday = date.withDayOfWeek(DateTimeConstants.MONDAY) .minusDays(1); 

... but it is not clear to me whether withDayOfWeek always go the previous Monday or next Monday. (This is what I'm going to find out in Noda Time and give options ...)

Instead, you can try the following:

 // Go back 1 day for Monday, 2 days for Tuesday etc LocalDate previousSunday = date.minusDays(date.getDayOfWeek()); 

Once you finish a completed week or month, you can easily get to the start:

 LocalDate startOfPreviousMonth = endOfPreviousMonth.withDayOfMonth(1); LocalDate startOfPreviousWeek = previousSunday.minusDays(6); 
+4
source

I did some research on JODA time, here are some useful results for reference.

  import org.joda.time.DateTime; import org.joda.time.DateTimeConstants; import org.joda.time.LocalDate; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class JodaDemo { public static final String DATE_PATTERN = "dd/MM/yyyy"; public static void main(String[] args) { DateTimeFormatter jf = DateTimeFormat.forPattern(DATE_PATTERN); //get first & last day of relative year System.out.println(jf.print(new LocalDate().dayOfYear().withMinimumValue())); System.out.println(jf.print(new LocalDate().dayOfYear().withMaximumValue())); //get first & last day of month System.out.println(jf.print(new LocalDate().dayOfMonth().withMinimumValue())); System.out.println(jf.print(new LocalDate().dayOfMonth().withMaximumValue())); //get first & last day of current week System.out.println(jf.print(new LocalDate().dayOfWeek().withMinimumValue())); System.out.println(jf.print(new LocalDate().dayOfWeek().withMaximumValue())); //or System.out.println("=="+jf.print(new LocalDate().withDayOfWeek(DateTimeConstants.MONDAY))); System.out.println("=="+jf.print(new LocalDate().withDayOfWeek(DateTimeConstants.SUNDAY))); //print todays date System.out.println(jf.print(DateTime.now())); // print 2 years before and after date System.out.println(jf.print(DateTime.now().minusYears(2))); System.out.println(jf.print(DateTime.now().plusYears(2))); // get first date of century era System.out.println(jf.print(DateTime.now().getCenturyOfEra())); } } 
+2
source

Source: https://habr.com/ru/post/919746/


All Articles