将日期格式从2012年1月1日转换为2012年1月1日inv java?
我想将日期格式从代码中的将日期格式从2012年1月1日转换为2012年1月1日inv java?,java,android,date,Java,Android,Date,我想将日期格式从代码中的1-1-2012转换为2012年1月1日。 有人能帮我吗?你可以使用SimpleDateFormat 您可以使用SimpleDataFormat 仔细阅读这节课 仔细阅读这节课 首先将输入字符串解析为日期,然后将其格式化为首选格式: String inputDateString = "15-1-2012"; DateFormat dfFrom = new SimpleDateFormat("dd-MM-yyyy"); Date inputDate = dfFrom
1-1-2012
转换为2012年1月1日
。
有人能帮我吗?你可以使用
SimpleDateFormat
您可以使用
SimpleDataFormat
仔细阅读这节课
仔细阅读这节课
首先将输入字符串解析为日期,然后将其格式化为首选格式:
String inputDateString = "15-1-2012";
DateFormat dfFrom = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dfFrom.parse(inputDateString);
DateFormat dfTo = new SimpleDateFormat("d MMMM yyyy");
String outputDate = dfTo.format(inputDate);
此外,如果需要本地化输出日期,则支持自定义区域设置。首先将输入字符串解析为日期,然后将其格式化为首选格式:
String inputDateString = "15-1-2012";
DateFormat dfFrom = new SimpleDateFormat("dd-MM-yyyy");
Date inputDate = dfFrom.parse(inputDateString);
DateFormat dfTo = new SimpleDateFormat("d MMMM yyyy");
String outputDate = dfTo.format(inputDate);
此外,如果您需要本地化输出日期,则支持自定义区域设置。尝试此操作
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.datepicker);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month+1).append("-").append(day).append("-")
.append(year).append(" "));
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
// set current date into datepicker
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
Calendar c = Calendar.getInstance();
c.set(selectedYear, selectedMonth, selectedDay);
year = selectedYear;
month = selectedMonth;
day = selectedDay;
string=c.getDisplayName(c.MONTH, LONG, Locale.US);
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(string).append("-").append(day).append("-")
.append(year).append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
Xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change " />
<TextView
android:id="@+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (MM-DD-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
试试这项工作
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.datepicker);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month+1).append("-").append(day).append("-")
.append(year).append(" "));
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
// set current date into datepicker
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
Calendar c = Calendar.getInstance();
c.set(selectedYear, selectedMonth, selectedDay);
year = selectedYear;
month = selectedMonth;
day = selectedDay;
string=c.getDisplayName(c.MONTH, LONG, Locale.US);
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(string).append("-").append(day).append("-")
.append(year).append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
Xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change " />
<TextView
android:id="@+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (MM-DD-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
谁将放弃投票指定的任何原因。首先,我们运行代码并进行投票。谁将放弃投票指定的任何原因。首先,我们运行代码并进行投票。