使用DateFormat类获取日期和日期字符串 – Android

我一直在阅读所有post,了解如何申请在我的项目中获取日期和日期,但一直无法弄明白(Java的新手)。

我有一个带有时间戳的按钮,但我必须将此时间戳转换为星期几,日期。 例如:2014年1月4日星期二。

日期应该是用户单击按钮的当前日期。 建议我们使用DateFormat类(目前还没有在我的片段文件中使用它),所以在答案中考虑一下。

但是,我已经编写了大部分代码,因此它必须与所有内容完美匹配,因此我无法解释过多。 针对这一挑战的任何想法? 我已经将Crime.java文件方法更改为DateFormat(它之前只使用了Date类),但是另一个主要的片段文件需要使用该信息,只是不知道如何去做。

这是我的主要片段文件,我正在构建时间码(时间部分):

@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); } 

在代码的中间行,需要改变一些东西。 目前它会显示一个时间戳,但我不知道为什么(它必须是在Date类上调用的getDate()的默认值?)

这是我单独的Crime类文件,包含我的所有实例(整个文件):

  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; } } 

由于你坚持使用DateFormat,它似乎不像SimpleDateFormat选项那样灵活。 不过,您可以使用它来满足您的原因,如下所示:

假设:你正在使用

 import java.text.DateFormat; 

你修改过的代码:

  @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); ... 

编辑:因为,您的DataFormatandroid.text.format.DateFormat类型将您的代码更改为以下:

  @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); ... 

我想你会对日期格式感到困惑,

来自Java Docs

DateFormat是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化和分析日期或时间。 日期/时间格式化子类(如SimpleDateFormat)允许格式化(即日期 – >文本),解析(文本 – >日期)和规范化。 日期表示为Date对象或自1970年1月1日00:00:00 GMT以来的毫秒数。

您需要定义DateFormat模式,就像在我的情况下它是dd-MMM-yyyy HH:mm:ss ,示例:

 private static ThreadLocal simpleDateFormatPool = new ThreadLocal(){ protected SimpleDateFormat initialValue() { return new SimpleDateFormat("EEEE dd-MMM-yyyy HH:mm:ss"); }; }; public static void main(String[] args) { // TODO Auto-generated method stub SimpleDateFormat dateFormat = null; if(dateFormat == null){ dateFormat = simpleDateFormatPool.get(); } System.out.println(dateFormat.format(new Date(System.currentTimeMillis()))); } 

OUTPUT

 Friday 10-Jan-2014 10:41:19 

你可以试试这个

 Date d1 =new Date(); SimpleDateFormat date = new SimpleDateFormat("EEEE, MMM'.', dd'th', yyyy"); System.out.println(date.format(d1)); OUTPUT: Tuesday, Jan. 4th, 2014. 

您可以尝试:

 String formattedDate = null; long timeInMillis = System.currentTimeMillis(); Calendar cal1 = Calendar.getInstance(); cal1.setTimeInMillis(timeInMillis); SimpleDateFormat dateFormat = new SimpleDateFormat( "EEEE, MMM, dd, yyyy"); formattedDate = dateFormat.format(cal1.getTime()); 

这将为您提供输出: Friday, Jan, 10, 2014

希望这能给出一些想法。