I want to go 1 year back with joda time, but I don't want to go from 15 February 2010 to 15 February 2009 but rather, if 15 Februari 2010 is a Monday for example, then I want to get to the closest Monday of 2009.
At the moment I am using this:
int dayNumber = Integer.parseInt(iDate.dayOfWeek().getAsString());
if(dayNumber == 1) { // start of week
println(iDate.plusYears(-1).plusDays(1).dayOfWeek().getAsString());
}
Which works fine for now but there is probably safer way that allows you to go back easily 5 years for example.
I would use Joda Time methods to go back 1 year and then have a little switch
statement that moves it to the appropriate day. Can't tell if you want to go to monday or not from your question. Doing your own calculations is going to miss important things like leap years as @Klas pointed out.
iDate = iDate.minusYears(1);
switch (iDate.getDayOfWeek()) {
case 1:
// monday so no change
break;
case 2:
iDate = iDate.minusDays(1);
break;
case 3:
iDate = iDate.minusDays(2);
break;
...
}
If you want to get fancy you can build a little grid to speed it up.
// days in joda time are 1 based so 0 won't be used here
private static final int[] daysShift = new int[] = {0, 0, -1, -2, -3, 3, 2, 1};
...
iDate = iDate.minusYears(1);
// based on the current day, shift us by a certain number of days, + or -
iDate.plusDays(daysShift[iDate.getDayOfWeek()]);
LocalDate.now( ZoneId.of( "Pacific/Auckland" ) )
.minusYears( 1 )
.previousOrSame( DayOfWeek.MONDAY )
The Joda-Time project is now in maintenance mode , with the team advising migration to the java.time classes.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without time zone.
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec .
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
The java.time classes can do math.
LocalDate yearAgo = today.minusYears( 1 ) ;
Determine today's day-of-week.
DayOfWeek dow = today.getDayOfWeek() ;
To adjust into different date-time values, use a TemporalAdjuster
implementation. One for your purpose is found in TemporalAdjusters
class.
LocalDate previousOrSameDay = yearAgo.with( TemporalAdjusters.previousOrSame( dow ) ;
Or get the following day-of-week.
LocalDate nextOrSameDay = yearAgo.with( TemporalAdjusters.nextOrSame( dow ) ;
You need to define your rules for going to next or previous.
Set< DayOfWeek > previous = EnumSet.range( DayOfWeek.MONDAY , DayOfWeek.THURSDAY ) ; // Collect Mon, Tue, Wed, and Thu.
Set< DayOfWeek > next = EnumSet.complementOf( previous) ; // Collect Fri, Sat, Sun.
Then test your day-of-week.
LocalDate ld ;
if( previous.contains( dow ) ) {
ld = yearAgo.with( TemporalAdjusters.previousOrSame( dow ) ;
} else if ( next.contains( dow ) ) {
ld = yearAgo.with( TemporalAdjusters.nextOrSame( dow ) ;
} else {
// Handle unexpected error.
System.out.println( "Unexpectedly reached IF-ELSE for day-of-week: " + dow ) ;
}
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.
To learn more, see the Oracle Tutorial . And search Stack Overflow for many examples and explanations. Specification is JSR 310 .
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more .
To start, #getAsString()
returns the value converted to a
String
usingInteger.toString
so it's rather silly to convert to a string, and then immediately convert back:
int dayNumber = iDate.dayOfWeek().get();
// or
int dayNumber = iDate.getDayOfWeek();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.