Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在Java中显示每月的周数_Android_Calendar_Jodatime - Fatal编程技术网

Android 如何在Java中显示每月的周数

Android 如何在Java中显示每月的周数,android,calendar,jodatime,Android,Calendar,Jodatime,我试图计算一个月的周数,但我很困惑我该怎么做。。。(我正在使用) 例如,我考虑了当前月份(输入变量): 我希望在输出中看到此结果(可能基于本地日期): 根据我在评论中发布的链接,以及我在评论中发布的关于plusDays和plusWeeks的提示,我使用Joda Time 2.3编写了以下示例 int month = DateTimeConstants.APRIL; int year = 2014; int dayOfWeek = DateTimeConstants.SUNDAY; Local

我试图计算一个月的周数,但我很困惑我该怎么做。。。(我正在使用)

例如,我考虑了当前月份(输入变量):

我希望在输出中看到此结果(可能基于本地日期):


根据我在评论中发布的链接,以及我在评论中发布的关于plusDays和plusWeeks的提示,我使用Joda Time 2.3编写了以下示例

int month = DateTimeConstants.APRIL;
int year = 2014;
int dayOfWeek = DateTimeConstants.SUNDAY;

LocalDate firstOfMonth = new LocalDate( year, month, 1 );
LocalDate firstOfNextMonth = firstOfMonth.plusMonths( 1 );
LocalDate firstDateInGrid = firstOfMonth.withDayOfWeek( dayOfWeek );
if ( firstDateInGrid.isAfter( firstOfMonth ) ) { // If getting the next start of week instead of desired week's start, adjust backwards.
    firstDateInGrid = firstDateInGrid.minusWeeks( 1 );
}

LocalDate weekStart = firstDateInGrid;
LocalDate weekStop = null;
int weekNumber = 0;

do {
    weekNumber = weekNumber + 1;
    weekStop = weekStart.plusDays( 6 );
    System.out.println( weekNumber + " week: " + weekStart + " --- " + weekStop );  // 1 week: 03-30-2014 --- 04-05-2014
    weekStart = weekStop.plusDays( 1 );
} while ( weekStop.isBefore( firstOfNextMonth ) );
当运行时

1周:2014-03-30--2014-04-05
两周:2014-04-06--2014-04-12
3周:2014-04-13--2014-04-19
4周:2014-04-20--2014-04-26
5周:2014-04-27--2014-05-03
至于以不同的方式格式化日期字符串,您可以在StackOverflow中搜索“joda format”,以找到许多示例


要使用一周中的某一天以本地化方式开始一周,请参阅。但通常最好询问用户。对于美国读者来说,请记住,美国在星期日开始的一周中占少数。

获取DateTime对象的可能重复项,然后调用
plusDays
plusWeeks
方法。关于StackOverflow的许多例子,如果你搜索的话,情况就不一样了,因为我需要管理上个月和下个月…当涉及到本地化的周计算时,JodaTime没有解决方案。您必须推出自己的、可能容易出错的解决方案。Java-8中的其他库(如JSR-310)更好(注意类
WeekFields
)。
int month = DateTimeConstants.APRIL;
int year = 2014;
int dayOfWeek = DateTimeConstants.SUNDAY;

LocalDate firstOfMonth = new LocalDate( year, month, 1 );
LocalDate firstOfNextMonth = firstOfMonth.plusMonths( 1 );
LocalDate firstDateInGrid = firstOfMonth.withDayOfWeek( dayOfWeek );
if ( firstDateInGrid.isAfter( firstOfMonth ) ) { // If getting the next start of week instead of desired week's start, adjust backwards.
    firstDateInGrid = firstDateInGrid.minusWeeks( 1 );
}

LocalDate weekStart = firstDateInGrid;
LocalDate weekStop = null;
int weekNumber = 0;

do {
    weekNumber = weekNumber + 1;
    weekStop = weekStart.plusDays( 6 );
    System.out.println( weekNumber + " week: " + weekStart + " --- " + weekStop );  // 1 week: 03-30-2014 --- 04-05-2014
    weekStart = weekStop.plusDays( 1 );
} while ( weekStop.isBefore( firstOfNextMonth ) );