Jod*_*hen 45
Joda-Time使用ISO标准周一至周日周.
它无法获得一周的第一天,也无法根据标准星期一以外的任何一天返回星期几索引.最后,总是根据ISO规则计算周数.
Mar*_*ouf 14
没有理由你不能使用JDK至少找到给定Locale的"一周的习惯性开始".唯一棘手的部分是翻译工作日的常量,其中两者都是1到7,但是java.util.Calendar被移一,Calendar.MONDAY = 2而DateTimeConstants.MONDAY = 1.
无论如何,使用此功能:
/**
* Gets the first day of the week, in the default locale.
*
* @return a value in the range of {@link DateTimeConstants#MONDAY} to
* {@link DateTimeConstants#SUNDAY}.
*/
private static final int getFirstDayOfWeek() {
return ((Calendar.getInstance().getFirstDayOfWeek() + 5) % 7) + 1;
}
Run Code Online (Sandbox Code Playgroud)
将一个Locale添加到Calendar.getInstance()以获取除默认值之外的某些Locale的结果.
以下是一个人如何解决Joda时间来获得美国本周的第一天:
DateTime getFirstDayOfWeek(DateTime other) {
if(other.dayOfWeek.get == 7)
return other;
else
return other.minusWeeks(1).withDayOfWeek(7);
}
Run Code Online (Sandbox Code Playgroud)
或者在斯卡拉
def getFirstDayOfWeek(other: DateTime) = other.dayOfWeek.get match {
case 7 => other
case _ => other.minusWeeks(1).withDayOfWeek(7)
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
33201 次 |
最近记录: |