我是新手android开发者。
现在,我尝试将日期格式从m / d / yyyy更改为mmddyyyy。
这是参考链接:http://www.thaicreate.com/mobile/android-calendarview.html
希望有人帮助我。
抱歉我的英语不好。
myCalendarView.java
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.widget.CalendarView;
import android.content.Intent;
public class myCalendarView extends Activity {
private int mYear;
private int mMonth;
private int mDay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CalendarView calendar = new CalendarView(this);
calendar.setOnDateChangeListener(mDateSetListener);
setContentView(calendar);
}
// the callback received when the user "sets" the date in the dialog
private CalendarView.OnDateChangeListener mDateSetListener =
new CalendarView.OnDateChangeListener() {
public void onSelectedDayChange(CalendarView view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
String selectedDate = new StringBuilder().append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear).append(" ").toString();
Bundle b = new Bundle();
b.putString("dateSelected", selectedDate);
//Add the set of extended data to the intent and start it
Intent intent = new Intent();
intent.putExtras(b);
setResult(RESULT_OK,intent);
finish();
}
};
}
MainActivity.java
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import java.util.Calendar;
import android.content.Intent;
public class MainActivity extends Activity {
private EditText editT1;
private Button btn1;
private int mYear;
private int mMonth;
private int mDay;
static final int CALENDAR_VIEW_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// editText1
editT1 = (EditText) findViewById(R.id.editText1);
// button1
btn1 = (Button) findViewById(R.id.button1);
// add a click listener to the button
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, myCalendarView.class);
startActivityForResult(intent,CALENDAR_VIEW_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateCurrentDate();
}
// updates the date we display in the TextView
private void updateCurrentDate() {
editT1.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case CALENDAR_VIEW_ID:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
editT1.setText(bundle.getString("dateSelected"));
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
答案 0 :(得分:0)
您可以像
一样进行转换DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
Date date = (Date)formatter.parse("5/1/12");
formatter = new SimpleDateFormat("MM/dd/yyyy");
date = (Date)formatter.format(date);
如果你想更改日期选择器,那么你可以使用这个
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
int month = monthOfYear + 1;
String formattedMonth = "" + month;
String formattedDayOfMonth = "" + dayOfMonth;
if(month < 10){
formattedMonth = "0" + month;
}
if(dayOfMonth < 10){
formattedDayOfMonth = "0" + dayOfMonth;
}
searchText.setText(formattedDayOfMonth + "/" + formattedMonth + "/" + year);
}
答案 1 :(得分:0)
如上所述,您可能最好使用简单的日期格式。
这是一个教程,将显示输出的差异。
https://www.ntu.edu.sg/home/ehchua/programming/java/DateTimeCalendar.html
答案 2 :(得分:0)
try {
SimpleDateFormat inputDate = new SimpleDateFormat("d/M/yyyy", Locale.US);
SimpleDateFormat outputDate = new SimpleDateFormat("MMddyyyy", Locale.US);
String date = outputDate.format(inputDate.parse("5/2/1993"));
} catch (ParseException e) {
e.printStackTrace();
}
答案 3 :(得分:0)
用户根据您的日期格式解析日期的方法..
public static String parsePickerDate(int Year, int Month, int day) {
String sYear = String.valueOf(Year);
String sMonth = String.valueOf((Month + 1));
String sDay = String.valueOf(day);
if (sDay.length() == 1)
sDay = "0" + sDay;
if (sMonth.length() == 1)
sMonth = "0" + sMonth;
if (sDay.length() == 1)
sDay = "0" + sDay;
return sYear + "-" + sMonth + "-" + sDay;
}