gpt4 book ai didi

android - 如何在 Android 中设置自定义格式日期

转载 作者:搜寻专家 更新时间:2023-11-01 07:49:53 25 4
gpt4 key购买 nike

我想用 Realm 数据库开发 TODO-List 应用程序,这个应用程序用户可以为 Task/Todo 设置标题、日期和时间。

我的代码是:
对话代码:

// Calendar Class
Calendar calender = Calendar.getInstance();
calender.set(Calendar.DAY_OF_MONTH, dialog_date.getDay());
calender.set(Calendar.MONTH, dialog_date.getMonth());
calender.set(Calendar.YEAR, dialog_date.getYear());
calender.set(Calendar.HOUR, dialog_date.getHour());
calender.set(Calendar.MINUTE, dialog_date.getMinute());

long now = System.currentTimeMillis();
Realm realm = Realm.getDefaultInstance();
Task_Provider task_provider = new Task_Provider(addTask, now, calender.getTimeInMillis(), false);

realm.beginTransaction();
realm.copyToRealm(task_provider);
realm.commitTransaction();
realm.close();

适配器代码:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof TaskHolder) {
TaskHolder taskHolder = (TaskHolder) holder;
Task_Provider task_provider = mResults.get(position);
taskHolder.setTask(task_provider.getTask());
taskHolder.setDate(task_provider.getWhen_date());
taskHolder.setBackground(task_provider.isComplete());

mAdapter = new TaskAdapter(mContext, mRealm, mResults, mMarkListener);
}
}

setDate 方法代码:

    public void setDate(long when_date) {
card_date.setText(DateUtils.getRelativeTimeSpanString(when_date, System.currentTimeMillis(), DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_ABBREV_ALL));
}
}


我的申请结果如下图:
In my Application

但是我想要像这张图片那样显示日期:
I want this image

如何设置日历日期的格式?全部发送 <3

最佳答案

添加到类(class)的顶部:

private static SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");

那么您的新 setDate() 将是:

public void setDate(long when_date) {
card_date.setText(dateFormat .format(new Date(when_date)));
}

关于android - 如何在 Android 中设置自定义格式日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36383951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com