将从 DatePickerDialog 获取的日期格式转换为 yyyy-MM-dd
Convert Date format getting from DatePickerDialog to yyyy-MM-dd
我想将日期格式转换为 yyyy-MM-dd 我从 DatePickerDialog 获取并将日期存储在编辑文本中,并将日期检索为字符串,我在字符串中获取的日期格式为mm-dd-yyyy
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, EnquiryAdapter.OnItemClickListener {
String StartDate = mStartDate.getText().toString();
String endDate = mEndDate.getText().toString();
private EditText mStartDate, mEndDate;
private void showFilterDialog(){
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View filterDialog = layoutInflater.inflate(R.layout.filters,null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
alertBuilder.setTitle("Filters");
alertBuilder.setView(filterDialog);
mStartDate = filterDialog.findViewById(R.id.editText);
mEndDate = filterDialog.findViewById(R.id.editText2);
String StartDate = mStartDate.getText().toString();
String endDate = mEndDate.getText().toString();
// i have tried this method but getting error
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String sd = format.format(java.sql.Date.parse(StartDate));
String ed = format.format(Date.parse(endDate));
Log.d("Start Date", sd);
Log.d("End Date", ed);
}
// 获取错误
java.lang.IllegalArgumentException
试试这个@Shubham
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calDate; = calDate = Calendar.getInstance();
calDate.add(Calendar.DAY_OF_YEAR, 0);
String date = simpleDateFormat.format(calDate.getTime());
Log.e("result_date",date);
试试这个可能对你有帮助
public static String parseDate(String inputDateString, SimpleDateFormat inputDateFormat, SimpleDateFormat outputDateFormat) {
Date date = null;
String outputDateString = null;
try {
date = inputDateFormat.parse(inputDateString);
outputDateString = outputDateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return outputDateString;
}
如何调用
public static final SimpleDateFormat inputFormat = new SimpleDateFormat("mm-dd-yyyy", Locale.getDefault());
public static final SimpleDateFormat outPutForamt = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
private String outputDateStr = "";
outputDateStr = parseDate("04-02-2019", inputFormat , outPutForamt );
Log.i("output_string", outputDateStr);
我想将日期格式转换为 yyyy-MM-dd 我从 DatePickerDialog 获取并将日期存储在编辑文本中,并将日期检索为字符串,我在字符串中获取的日期格式为mm-dd-yyyy
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, EnquiryAdapter.OnItemClickListener {
String StartDate = mStartDate.getText().toString();
String endDate = mEndDate.getText().toString();
private EditText mStartDate, mEndDate;
private void showFilterDialog(){
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View filterDialog = layoutInflater.inflate(R.layout.filters,null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
alertBuilder.setTitle("Filters");
alertBuilder.setView(filterDialog);
mStartDate = filterDialog.findViewById(R.id.editText);
mEndDate = filterDialog.findViewById(R.id.editText2);
String StartDate = mStartDate.getText().toString();
String endDate = mEndDate.getText().toString();
// i have tried this method but getting error
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String sd = format.format(java.sql.Date.parse(StartDate));
String ed = format.format(Date.parse(endDate));
Log.d("Start Date", sd);
Log.d("End Date", ed);
}
// 获取错误 java.lang.IllegalArgumentException
试试这个@Shubham
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar calDate; = calDate = Calendar.getInstance();
calDate.add(Calendar.DAY_OF_YEAR, 0);
String date = simpleDateFormat.format(calDate.getTime());
Log.e("result_date",date);
试试这个可能对你有帮助
public static String parseDate(String inputDateString, SimpleDateFormat inputDateFormat, SimpleDateFormat outputDateFormat) {
Date date = null;
String outputDateString = null;
try {
date = inputDateFormat.parse(inputDateString);
outputDateString = outputDateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return outputDateString;
}
如何调用
public static final SimpleDateFormat inputFormat = new SimpleDateFormat("mm-dd-yyyy", Locale.getDefault());
public static final SimpleDateFormat outPutForamt = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
private String outputDateStr = "";
outputDateStr = parseDate("04-02-2019", inputFormat , outPutForamt );
Log.i("output_string", outputDateStr);