3 回答
TA贡献1934条经验 获得超2个赞
好吧,以19/01/1970为例。从中减去7天,仍然是一月。再次减去7天,..仍在一月。第4次减去7天,..哦,它不再是一月了。您第四次删除7天,它不再是正确的月份。
这就是您所需要的。
相关方法:for循环,计数器和..minusDays(7)
方法,以及getMonth()
。
TA贡献1827条经验 获得超8个赞
ChronoField.ALIGNED_WEEK_OF_MONTH
您可以使用LocalDate.get()和ChronoField.ALIGNED_WEEK_OF_MONTH。
import java.time.LocalDate;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
/**
*
* @author Sedrick
*/
public class JavaApplication19 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
LocalDate localDate1 = LocalDate.of(1970, 01, 01);
LocalDate localDate2 = LocalDate.of(1970, 02, 01);
LocalDate localDate3 = LocalDate.of(1970, 01, 19);
LocalDate localDate4 = LocalDate.of(1970, 01, 31);
System.out.println(localDate1.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
System.out.println(localDate2.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
System.out.println(localDate3.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
System.out.println(localDate4.get(ChronoField.ALIGNED_WEEK_OF_MONTH));
System.out.println(getOrdinalPosition(localDate4));
}
static public int getOrdinalPosition(TemporalAccessor temporal) {
return LocalDate.from(temporal).get(ChronoField.ALIGNED_WEEK_OF_MONTH);
}
}
输出:
run:
1
1
3
5
5
BUILD SUCCESSFUL (total time: 0 seconds)
TA贡献1836条经验 获得超3个赞
您需要的是 ChronoField.ALIGNED_WEEK_OF_MONTH
LocalDate.of(1970,1,1).get(ChronoField.ALIGNED_WEEK_OF_MONTH) //1
LocalDate.of(1970,1,2).get(ChronoField.ALIGNED_WEEK_OF_MONTH) //1
LocalDate.of(1970,1,19).get(ChronoField.ALIGNED_WEEK_OF_MONTH) //3
LocalDate.of(1970,1,31).get(ChronoField.ALIGNED_WEEK_OF_MONTH) //5
添加回答
举报