在Android中设置日历字段 [英] Setting Calendar fields in Android

查看:188
本文介绍了在Android中设置日历字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

  myCalender.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY)在日历中设置日期字段


是否将日期设置为下一个星期五或过去的星期五,或者最接近的星期五? p>

如果我没有为日历设置年份或其他任何字段,默认值是什么?

解决方案

都没有。然而。情况很复杂。但是请不要使用日历,请参见下文。



来自两个参数 set的文档方法


将给定日历字段设置为给定值。值不是用此方法解释的
...


因此在此调用期间,它仅设置了星期几字段到星期五。年,月和月中的日期保持不变,即日期没有得到有效更改。



如果以及何时日历最终计算并解析其字段,


如果日历字段值存在任何冲突,则 Calendar 会将
优先级赋予已最近设置。以下的
是日历字段的默认组合。将使用由最近设置的单个
字段确定的
最近组合。


如果仅设置星期几(可能会与其他字段发生冲突),则适用以下组合:


YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK


因此它将在



定义一周的时间取决于日历的第一天领域。它的初始值由 Calendar 的语言环境确定,您可以通过 getFirstDayOfWeek() setFirstDayOfWeek()。反过来,这意味着以前设置为星期日的欧洲语言环境中日历的默认行为将返回到上一个星期五,而日历在美国语言环境中会改为选择下一个星期五。



如果您在设置星期几之后但之前还设置了其他字段



默认值?



通常默认值 Calendar 字段是JVM时区中的当前日期和时间(通常但并非总是与设备时区相同)。



您不需要关心



好消息是您不需要关心。 Calendar 类无论如何早就过时了,并且(回顾起来)它的设计也很差,所以您不应该使用它。而是使用 java.time (现代的Java日期和时间API)。与之合作真是太好了。例如,根据您的需要,您可以使用 LocalDate 作为日期,或者使用 ZonedDateTime 作为日期和时间,时区。



现代类为您提供了更好的清晰度和灵活性。例如:

  LocalDate ld = LocalDate.now(ZoneId.of( Pacific / Saipan)); 

//以下调用执行方法名称所说的
ld = ld.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
ld = ld.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
ld = ld.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
ld = ld.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY));

//设置为同一ISO周的星期五
ld = ld.with(ChronoField.DAY_OF_WEEK,DayOfWeek.FRIDAY.getValue());

//设置为同一美国周的星期五
ld = ld.with(WeekFields.SUNDAY_START.dayOfWeek(),
DayOfWeek.FRIDAY.get(WeekFields.SUNDAY_START。 dayOfWeek()));



问题:我可以使用 java.time



是的, java.time 在Android设备上可以很好地工作。它只需要至少 Java 6




  • 在Java 8和更高版本以及新的Android设备上(通过API)有人告诉我,第26级是内置的新API。

  • 在Java 6和7中,获得了ThreeTen Backport,即新类的Backport(JSR 310的ThreeTen,其中

  • 在(较旧的)Android上,使用Android版的ThreeTen Backport。叫做ThreeTenABP。确保从包 org.threeten.bp 和子包中导入日期和时间类。



链接




setting the day field in a calendar using

    myCalender.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

does is put set the day to the next Friday or the past Friday or maybe the closest?

Also what if I don't set the year or any other field for the calendar, what are the default values?

解决方案

Neither. Yet. It’s complicated. But don’t use Calendar, see below.

From the documentation of the two-arg set method:

Sets the given calendar field to the given value. The value is not interpreted by this method…

So during this call it just sets the day-of-week field to Friday. The year, month and day-of-month stay the same, that is, the date isn’t effectively changed. Yet.

If and when the Calendar eventually computes and resolves its fields,

If there is any conflict in calendar field values, Calendar gives priorities to calendar fields that have been set more recently. The following are the default combinations of the calendar fields. The most recent combination, as determined by the most recently set single field, will be used.

If you have only set the day of week (which will likely have introduced a conflict with the other fields), the following combination applies:

YEAR + MONTH + WEEK_OF_MONTH + DAY_OF_WEEK

So it will pick the Friday in the same week.

What defines a week is determined by the Calendar’s first-day-of-week field. Its initial value is determined by the Calendar’s locale, and you can control it through getFirstDayOfWeek() and setFirstDayOfWeek(). This in turn means that the default behaviour for a Calendar in a European locale previously set to a Sunday will go back to the previous Friday, whereas a Calendar in the US locale would pick the next Friday instead.

If you have also set other fields after setting the day-of-week but before fields are calculated, it is a different story.

Default values?

Generally the default values of Calendar fields are current date and time in your JVM’s time zone (usually, but not always, the same as your device’s time zone).

You don’t need to care

The good news is that you don’t need to care. The Calendar class is long outmoded anyway, and it was also (seen in retrospect) poorly designed, so you should not use it. Instead use java.time, the modern Java date and time API. It is so much nicer to work with. Depending on your need, you may for example use LocalDate for a date or ZonedDateTime for a date and time of day with time zone.

The modern classes offer you a great deal of better clarity and more flexibility. For example:

    LocalDate ld = LocalDate.now(ZoneId.of("Pacific/Saipan"));

    // the following calls do what the method names say
    ld = ld.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
    ld = ld.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY));
    ld = ld.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));
    ld = ld.with(TemporalAdjusters.previousOrSame(DayOfWeek.FRIDAY));

    // set to Friday in the same ISO week
    ld = ld.with(ChronoField.DAY_OF_WEEK, DayOfWeek.FRIDAY.getValue());

    // set to Friday in the same US week
    ld = ld.with(WeekFields.SUNDAY_START.dayOfWeek(), 
            DayOfWeek.FRIDAY.get(WeekFields.SUNDAY_START.dayOfWeek()));

Question: Can I use java.time on Android?

Yes, java.time works nicely on Android devices. It just requires at least Java 6.

  • In Java 8 and later and on new Android devices (from API level 26, I’m told) the new API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310, where the modern API was first described).
  • On (older) Android, use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. Make sure you import the date and time classes from package org.threeten.bp and subpackages.

Links

这篇关于在Android中设置日历字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆