问题
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;
}
}
回答1:
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);
...
回答2:
I think so you are getting confused with Date format,
From Java Docs
DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date -> text), parsing (text -> date), and normalization. The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.
You need to define DateFormat
pattern like in my case it is dd-MMM-yyyy HH:mm:ss
, example :
private static ThreadLocal<SimpleDateFormat> simpleDateFormatPool = new ThreadLocal<SimpleDateFormat>(){
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
回答3:
you can try this
Date d1 =new Date();
SimpleDateFormat date = new SimpleDateFormat("EEEE, MMM'.', dd'th', yyyy");
System.out.println(date.format(d1));
OUTPUT:
Tuesday, Jan. 4th, 2014.
回答4:
You can try it as:
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());
This will give you output: Friday, Jan, 10, 2014
Hope this gives some idea.
来源:https://stackoverflow.com/questions/21036410/get-day-date-string-using-dateformat-class-android