获取日间及放大器;使用DateFormat类日期字符串 - Android电子 [英] Get day & date string using DateFormat class - Android

查看:381
本文介绍了获取日间及放大器;使用DateFormat类日期字符串 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读通过所有的职位如何申请获得在我的项目和日期,但一直没能弄明白(很新到Java)。

我有上有一个时间戳的按钮,但我必须在此时间戳记转换为一周中的某一天,日期。例如这样的:周二,2014年1月4日

当用户点击该按钮的日期应该是当前日期。 有人建议我们使用DateFormat类(目前没有使用它在我的片段文件还),因此认为,在一个答案。

不过,我有我的大部分code的已经写好,所以它必须适应好一切,所以我不能解构太多。任何想法对这个挑战?我已经改变了Crime.java文件的方法,以日期格式(它只是利用Date类之前),但其他主要片段文件需要使用这些信息,只是不知道如何去做。

这是我的主要片段文件,我在建设的时候code(它的时间部分):

  @覆盖
    公共查看onCreateView(LayoutInflater吹气,ViewGroup中父母,捆绑savedInstanceState){mDateButton =(按钮)v.findViewById(R.id.crime_date);
        mDateButton.setText(mCrime.getDate()的toString());
        mDateButton.setEnabled(假);}

这是在code的东西需要改变的中间线。目前,它带来了一个时间戳,但我不知道为什么(它必须是呼吁Date类GETDATE()的默认?)

这是我独立的犯罪类文件,用我所有的实例(整个文件):

 公共类犯罪{        私人UUID MID;
        私人字符串mTitle;
        私人日期格式mDate;
        私人布尔mSolved;        公共犯罪(){
            //生成唯一标识符
            MID = UUID.randomUUID();
            mDate =新的DateFormat();
        }        公共字符串的getTitle(){
            返回mTitle;
        }        公共无效的setTitle(字符串名称){
            mTitle =称号;
        }        公共UUID的getId(){
            返回MID;
        }        公开日期格式GETDATE(){
            返回mDate;
        }        公共无效的setDate(日期格式日期){
            mDate =日期;
        }        公共布尔溶解情况(){
            返回mSolved;
        }        公共无效setSolved(布尔解决){
            mSolved =解决;
        }
    }


解决方案

既然你被卡住日期格式,它似乎并不像SimpleDateFormat的选择灵活。不过,你可以用它来满足你的事业象下面这样:

假设:
您正在使用

 导入java.text.DateFormat中;

您修改code:

  @覆盖
    公共查看onCreateView(LayoutInflater吹气,ViewGroup中父母,捆绑savedInstanceState){
     mDateButton =(按钮)v.findViewById(R.id.crime_date);
     mCrime.setDate(DateFormat.getDateInstance(DateFormat.FULL));
     mDateButton.setText(mCrime.getDate()格式(新的Date()));
     mDateButton.setEnabled(假);
...

编辑:
因为,你的 DATAFORMAT 的类型为 android.text.format.DateFormat 更改code到以下内容:

  @覆盖
    公共查看onCreateView(LayoutInflater吹气,ViewGroup中父母,捆绑savedInstanceState){
     mDateButton =(按钮)v.findViewById(R.id.crime_date);
     mDateButton.setText(android.text.format.DateFormat.format(EEEE,MMM DD YYYY,新的java.util.Date()));
     mDateButton.setEnabled(假);
...

I have been reading through all the posts for how to apply getting a day and date in my project, but haven't been able to figure it out (very new to Java).

I have a button with a timestamp on it, but I must convert this timestamp into a day of the week, with date. For example like this: Tuesday, Jan. 4th, 2014.

The date should be the current date when the user clicks the button. It was recommended that we use the DateFormat class (not currently using it in my fragment file yet), so consider that in an answer.

However, I have most of my code already written, so it must fit in well with everything, so I can't deconstruct too much. Any ideas for this challenge? I already changed the Crime.java file method to DateFormat (it was just using Date class before), but the other main fragment file will need to use that info, just not sure how to do it.

Here is my main fragment file I'm building the time code in (time portion of it):

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

mDateButton = (Button)v.findViewById(R.id.crime_date);
        mDateButton.setText(mCrime.getDate().toString());
        mDateButton.setEnabled(false);

}

It is in the middle line of code that something needs to change. Currently it brings up a timestamp, but I don't know why (it must be the default of getDate() called on the Date class?)

Here is my separate Crime class file, with all my instances (whole file):

 public class Crime {

        private UUID mId;
        private String mTitle;
        private DateFormat mDate;
        private boolean mSolved;

        public Crime() {
            //Generate unique identifier
            mId = UUID.randomUUID();
            mDate = new DateFormat();
        }

        public String getTitle() {
            return mTitle;
        }

        public void setTitle(String title) {
            mTitle = title;
        }

        public UUID getId() {
            return mId;
        }

        public DateFormat getDate() {
            return mDate;
        }

        public void setDate(DateFormat date) {
            mDate = date;
        }

        public boolean isSolved() {
            return mSolved;
        }

        public void setSolved(boolean solved) {
            mSolved = solved;
        }


    }

解决方案

Since you are stuck with DateFormat, it does not seem to be as flexible as SimpleDateFormat option. Still, you can use it to suit your cause like below:

Assumption: You are using

import java.text.DateFormat;

Your modified code:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
     mDateButton = (Button)v.findViewById(R.id.crime_date);
     mCrime.setDate(DateFormat.getDateInstance(DateFormat.FULL));
     mDateButton.setText(mCrime.getDate().format(new Date()));
     mDateButton.setEnabled(false);
...

EDIT: Since, your DataFormat is of type android.text.format.DateFormat change your code to following:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
     mDateButton = (Button)v.findViewById(R.id.crime_date);
     mDateButton.setText(android.text.format.DateFormat.format("EEEE, MMM dd yyyy", new java.util.Date()));
     mDateButton.setEnabled(false);
...

这篇关于获取日间及放大器;使用DateFormat类日期字符串 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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