Apache DateUtils truncates in a week

I use the Apache method commons-lang3 DateUtils.truncate(Calendar calendar, int field) to "crop" the unnecessary fields of the Calendar object. Now, when the field parameter gets the value Calendar.WEEK_OF_MONTH , it returns

java.lang.IllegalArgumentException: The field 4 is not supported

The documentation for the truncate() method reads:

 /** * <p>Truncates a date, leaving the field specified as the most * significant field.</p> * * <p>For example, if you had the date-time of 28 Mar 2002 * 13:45:01.231, if you passed with HOUR, it would return 28 Mar * 2002 13:00:00.000. If this was passed with MONTH, it would * return 1 Mar 2002 0:00:00.000.</p> * * @param date the date to work with, not null * @param field the field from {@code Calendar} or <code>SEMI_MONTH</code> * @return the different truncated date, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws ArithmeticException if the year is over 280 million */ 

Therefore, I suppose this should work, but this is clearly not the case. Is there a way to truncate dates on the first day of the week using DateUtils?


UPDATE

I looked in the source code and found out that the modify() method ( tuncate() uses this internally) iterates through a bunch of predefined fields to find this parameter. Now these fields:

 private static final int[][] fields = { {Calendar.MILLISECOND}, {Calendar.SECOND}, {Calendar.MINUTE}, {Calendar.HOUR_OF_DAY, Calendar.HOUR}, {Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.AM_PM /* Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK, Calendar.DAY_OF_WEEK_IN_MONTH */ }, {Calendar.MONTH, DateUtils.SEMI_MONTH}, {Calendar.YEAR}, {Calendar.ERA}}; 

As you can see, nothing is related to Calendar WEEK-ish fields, so I think I need to do this manually ... Any other thoughts / recommendations are welcome!

+5
source share
2 answers

It is impossible to truncate for a week in a reasonable way. Consider the following date:

 2014-11-01 12:01:55 2014-11-01 12:01:00 // truncate minute 2014-11-01 00:00:00 // truncate day 2014-11-00 00:00:00 // truncate month 

The starting date is Saturday. So what does truncating a week mean in this case? Should we truncate the previous Monday? If so, it will be:

 2014-10-27 00:00:00 // truncate week? 

This does not seem right to me. We changed the month in this case; sometimes even the year will change. If you can come up with a reasonable way to describe this (and some use cases), write down the problem , and we will cover It. But it hits me like a field that doesn't make sense for truncation.

You can find some ideas for your original problem: Get the current date on Monday per week.

+3
source

TL; DR

 LocalDate.now( ZoneId.of( "Africa/Tunis" ) ).with( TemporalAdjuster.previousOrSame​( DayOfWeek.MONDAY ) ) 

java.time

No need for an external library like DateUtils . Use the java.time classes built into Java 8 and later.

It seems like your real question is how to get the first day of the week for a specific date.

Timezone

The time zone is critical for determining the date. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec .

Specify the time zone name in continent/region format, such as America/Montreal , Africa/Casablanca or Pacific/Auckland . Never use pseudo-zones with 3-4 letters, such as EST or IST , as they are not real time zones, and are not standardized or even unique (!).

 ZoneId z = ZoneId.of( "America/Montreal" ) ; 

The LocalDate class represents a date value only without time and without a time zone.

 LocalDate today = LocalDate.now( z ); 

To get the first day of the week for this date, we must determine the first day of the week. This definition is culture dependent. In the United States, we usually mean Sunday. Most of the rest of the world means Monday. Whatever it is for your users, specify with the DayOfWeek enum object.

To go from our date to this date, use the TemporalAdjuster implemented in the TemporalAdjusters class.

 LocalDate firstOfWeek = today.with( TemporalAdjuster.previousOrSame​( DayOfWeek.MONDAY ) ) ; 
+2
source

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


All Articles