首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Android操作系统中,对于DatePicker中的setFirstDayOfWeek(),在Harmony OS中的替代方法是什么?

在Android操作系统中,对于DatePicker中的setFirstDayOfWeek(),在Harmony OS中的替代方法是什么?
EN

Stack Overflow用户
提问于 2021-07-29 14:02:28
回答 1查看 53关注 0票数 2

我正在编写一个自定义组件,允许用户使用JAVA SDK从日历中选择日期。我想将一天设置为一周的第一天(例如星期一),然后显示日历,并将该天作为一周的第一天(日历的第一列将是设置的那一天)。

在Android中,我们在DatePicker中有setFirstDayOfWeek(int firstDayOfWeek)方法,它可以为我们做到这一点。

在Harmony OS的DatePicker中有什么替代方案?

向您致敬,Subham

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-30 03:38:35

目前,还没有直接实现该功能的apis接口,但您可以使用工具类代码来实现该功能。如下所示:

代码语言:javascript
复制
public static LocalDateTime getMondayForThisWeek(LocalDate localDate) {
       LocalDateTime monday = LocalDateTime.of(localDate, LocalTime.MIN).with(DayOfWeek.MONDAY);
       return monday;
   }

参考代码:

工具类的核心代码:

代码语言:javascript
复制
public class LocalDateTimeUtil {

...

    /***

     * constructor method

     */

    private LocalDateTimeUtil() {

    }



    /**

     * Obtains the date of the Monday of the week to which the specified date belongs.
     *

     * @param localDate Time
     * @return LocalDateTime

     */

    public static LocalDateTime getMondayForThisWeek(LocalDate localDate) {

        LocalDateTime monday = LocalDateTime.of(localDate, LocalTime.MIN).with(DayOfWeek.MONDAY);

        return monday;

    }



    /**

     * Obtains the date of the Sunday of the week to which the specified date belongs.

     *

     * @param localDate Time

     * @return LocalDateTime

     */

    public static LocalDateTime getSundayForThisWeek(LocalDate localDate) {

        LocalDateTime sunday = LocalDateTime.of(localDate, LocalTime.MIN).with(DayOfWeek.SUNDAY);

        return sunday;

    }

...

}

XML文件布局:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>

<DirectionalLayout

    xmlns:ohos="http://schemas.huawei.com/res/ohos"

    ohos:height="match_parent"

    ohos:width="match_parent"

    ohos:orientation="vertical"

    >

   <DatePicker

       ohos:id="$+id:data_picker"

       ohos:height="match_content"

       ohos:width="match_parent"

       ohos:background_element="#C89FDEFF"

       />

</DirectionalLayout>

AbilitySlice中的代码:

代码语言:javascript
复制
public class MainAbilitySlice extends AbilitySlice {

    private DatePicker datePicker;

    @Override

    public void onStart(Intent intent) {

        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_ability_main);

        initView();

    }



    private void initView() {

        if (findComponentById(ResourceTable.Id_data_picker) instanceof DatePicker) {

            datePicker = (DatePicker) findComponentById(ResourceTable.Id_data_picker);

        }

        if (datePicker != null) {

            // If you select Monday or Sunday as the first day of a week, select the corresponding method. This example selects Monday as the first day of the week

            LocalDateTime mondayForThisWeek = LocalDateTimeUtil.getMondayForThisWeek(LocalDate.now());

            datePicker.updateDate(mondayForThisWeek.getYear(), mondayForThisWeek.getMonth().getValue(), mondayForThisWeek.getDayOfMonth());

        }

    }

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68570836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档