我创建了一个名为' DatePickerFragment'因此,用户可以从中选择日期,并且它将显示在按钮中;
如何更改格式? 目前的格式如下:" THU AUG 24 00:00:00 GMT + 04:30 2017"
我想忽略时间,只是这样表现出来:" 08-04-2017"
这是我的DatePickerFragment:
public class DatePickerFragment extends DialogFragment {
public static final String EXTRA_DATE = "com.drgnme.listhamrah.date";
private static final String ARG_DATE = "date";
private DatePicker mDatePicker;
public static DatePickerFragment newInstance (Date date) {
Bundle args = new Bundle();
args.putSerializable(ARG_DATE, date);
DatePickerFragment fragment = new DatePickerFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog (Bundle savedInstanceState) {
Date date = (Date) getArguments().getSerializable(ARG_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_date, null);
mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker);
mDatePicker.init(year, month, day, null);
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
// .setPositiveButton(android.R.string.ok, null)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
@Override
public void onClick (DialogInterface dialog, int which) {
int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();
Date date = new GregorianCalendar(year, month, day).getTime();
sendResult(Activity.RESULT_OK, date);
}
})
.create();
}
private void sendResult (int resultCode, Date date) {
if (getTargetFragment() == null) {
return;
}
Intent intent = new Intent();
intent.putExtra(EXTRA_DATE, date);
getTargetFragment()
.onActivityResult(getTargetRequestCode(), resultCode, intent);
}
}
这是片段包含按钮设置日期:
public class DetailFragment extends Fragment {
private static final String ARG_DETAIL_ID = "detail_id";
private static final String DIALOG_DATE = "DialogDate";
private static final String DIALOG_PHOTO = "DialogPhoto";
private static final int REQUEST_DATE = 0;
private static final int REQUEST_PHOTO = 2;
private Detail mDetail;
private File mPhotoFile;
private EditText mTitleField;
private EditText mDescriptionField;
private Button mDateButton;
private RatingBar mRatingBar;
private Button mReportButton;
private ImageButton mPhotoButton;
private ImageView mPhotoView;
public static DetailFragment newInstance (UUID detailId) {
Bundle args = new Bundle();
args.putSerializable(ARG_DETAIL_ID, detailId);
DetailFragment fragment = new DetailFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
UUID detailId = (UUID) getArguments().getSerializable(ARG_DETAIL_ID);
mDetail = DetailLab.get(getActivity()).getDetail(detailId);
mPhotoFile = DetailLab.get(getActivity()).getPhotoFile(mDetail);
}
@Override
public void onPause() {
super.onPause();
DetailLab.get(getActivity()).updateDetail(mDetail);
}
@Override
public View onCreateView (LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_detail,
container, false);
mTitleField = (EditText) v.findViewById(R.id.detail_title);
mTitleField.setText(mDetail.getTitle());
mTitleField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s,
int start, int before, int count) {
mDetail.setTitle(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
mDescriptionField = (EditText) v.findViewById(R.id.detail_description);
mDescriptionField.setText(mDetail.getDes());
mDescriptionField.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s,
int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s,
int start, int before, int count) {
mDetail.setDes(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
mDateButton = (Button) v.findViewById(R.id.detail_date);
updateDate();
mDateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = getFragmentManager();
DatePickerFragment dialog = DatePickerFragment
.newInstance(mDetail.getDate());
dialog.setTargetFragment(DetailFragment.this, REQUEST_DATE);
dialog.show(manager, DIALOG_DATE);
}
});
mRatingBar = (RatingBar) v.findViewById(R.id.ratingBar);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar,
float rating,
boolean fromUser) {
}
});
mReportButton = (Button) v.findViewById(R.id.detail_report);
mReportButton.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_TEXT, getDetailReport());
i.putExtra(Intent.EXTRA_SUBJECT,
getString(R.string.detail_report_subject));
i = Intent.createChooser(i, getString(R.string.send_report));
startActivity(i);
}
});
PackageManager packageManager = getActivity().getPackageManager();
mPhotoButton = (ImageButton) v.findViewById(R.id.detail_camera);
final Intent captureImage =
new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
boolean canTakePhoto = mPhotoFile != null &&
captureImage.resolveActivity(packageManager) != null;
mPhotoButton.setEnabled(canTakePhoto);
mPhotoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
Uri uri = FileProvider.getUriForFile(getActivity(),
"com.drgnme.listhamrah.fileprovider",
mPhotoFile );
captureImage.putExtra(MediaStore.EXTRA_OUTPUT, uri);
List<ResolveInfo> cameraActivities = getActivity()
.getPackageManager().queryIntentActivities(captureImage,
PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo activity : cameraActivities) {
getActivity().grantUriPermission(activity.activityInfo.packageName,
uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
startActivityForResult(captureImage, REQUEST_PHOTO);
}
});
mPhotoView = (ImageView) v.findViewById(R.id.detail_photo);
mPhotoView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
if (mPhotoFile == null || !mPhotoFile.exists()) return; //no photo
FragmentManager manager = getFragmentManager();
PhotoZoomFragment dialog = new PhotoZoomFragment()
.newInstance(mPhotoFile.getPath());
dialog.show(manager, DIALOG_PHOTO);
}
});
updatePhotoView();
return v;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onCreateOptionsMenu (Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_detail, menu);
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId()) {
case R.id.delete_detail:
DetailLab.get(getActivity()).deleteDetail(mDetail);
getActivity().finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onActivityResult (int requestCode,
int resultCode,
Intent data) {
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_DATE) {
Date date = (Date) data
.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
mDetail.setDate(date);
updateDate();
} else if (requestCode == REQUEST_PHOTO) {
Uri uri = FileProvider.getUriForFile(getActivity(),
"com.drgnme.listhamrah.fileprovider",
mPhotoFile);
getActivity().revokeUriPermission(uri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
updatePhotoView();
}
}
private void updateDate() {
mDateButton.setText(mDetail.getDate().toString());
}
private String getDetailReport() {
String dateFormat = "EEE, MMM dd";
String dateString = DateFormat.format(
dateFormat, mDetail.getDate()).toString();
String rateString = null;
String report = getString(R.string.detail_report,
mDetail.getTitle(), dateString,
rateString, rateString
);
return report;
}
private void updatePhotoView() {
if (mPhotoFile == null || !mPhotoFile.exists()) {
mPhotoView.setImageDrawable(null);
} else {
Bitmap bitmap = PictureUtils.getScaledBitmap(
mPhotoFile.getPath(), getActivity());
mPhotoView.setImageBitmap(bitmap);
}
}
}
答案 0 :(得分:1)
在您的活动结果中:
Date date = (Date)intent.getExtras().get(EXTRA_DATE);
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
String timeStr = format.format(date);
timeStr
将根据您的需要包含格式化日期。
答案 1 :(得分:0)
试试这个
String date="THU AUG 24 00:00:00 GMT+04:30 2017";
SimpleDateFormat spf=new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date newDate= null;
try {
newDate = spf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
spf= new SimpleDateFormat("dd/mm/yyyy");
date = spf.format(newDate);
Log.e("date",date);
答案 2 :(得分:0)
使用其他答案&#39;方法updateDate()
中的格式,如下所示:
private void updateDate() {
mDateButton.setText(new SimpleDateFormat("MM-dd-yyyy").format(mDetail.getDate()));
}
答案 3 :(得分:0)
我应该在我的updateDate()
方法中使用&#34; SimpleDateFormat(&#34; xxxxx ..&#34;),
它对我有用:
private void updateDate() {
mDateButton.setText(new SimpleDateFormat("MM-dd-yyyy").format(mDetail.getDate())); }
答案 4 :(得分:-1)
您可以尝试使用SimpleDateFormat格式化时间。 如:
SimpleDateFormat sf = new SimpleDateFormat("MM-dd-yyyy");
String result = sf.format("you data");