Java 在android中转换日期格式?
我在做日历日期选择器。我得到的日期格式为Java 在android中转换日期格式?,java,android,calendar,Java,Android,Calendar,我在做日历日期选择器。我得到的日期格式为2013/7/7,但我希望格式为2013/07/07 代码: 使用日期格式化程序 String date="2013/7/7"; SimpleDateFormat df=new SimpleDateFormat("yyyy/MM/dd"); System.out.println(df.format(df.parse(date))); 你做事很辛苦。使用,您可以更容易地编写、阅读和维护代码。尽量不要重新发明轮子 示例代码如下所示:
2013/7/7
,但我希望格式为2013/07/07
代码:
使用日期格式化程序
String date="2013/7/7";
SimpleDateFormat df=new SimpleDateFormat("yyyy/MM/dd");
System.out.println(df.format(df.parse(date)));
你做事很辛苦。使用,您可以更容易地编写、阅读和维护代码。尽量不要重新发明轮子 示例代码如下所示:
String date_string = new SimpleDateFormat("yyyy/MM/dd").format(original_date);
当然,如果需要多次执行该操作,您可以重用SimpleDateFormat
的实例 检查这是否有帮助
//Calender Variables
int mYear;
int mDay;
int mHour;
int mMinute;
c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH)+1;
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
TextView t = (TextView)findViewById(R.id.time);
TextView d = (TextView)findViewById(R.id.date);
d.setText("Date: " + mDay+" / "+mMonth + " / "+mYear+" ");
t.setText("Time: " + mHour+" : "+mMinute);
date = d.getText().toString();
time = t.getText().toString();
您可以使用以下方式使用
SimpleDateFormat
格式化日期:
String format = "yyyy/MM/dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse("12/31/2013"); // this date is in format MM/dd/yyyy and it will formatted to the format yyyy/MM/dd ( 2013/12/31 )
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
// formatting
System.out.println(sdf.format(new Date())); // this will format the current date to the format yyyy/MM/dd
将日历实例传递到DatePickerDialog中,然后根据需要使用SimpleDataFormat进行格式化
private DatePickerDialog.OnDateSetListener mDateSetListene = new DatePickerDialog.OnDateSetListener() {
// onDateSet method
public void onDateSet(DatePicker view, Calendar cal) {
dob.setText(new SimpleDateFormat("yyyy/MM/dd").format(cal.getTime()));
}
};
String strDate=“2013/7/7”;
字符串[]strsplit=strDate.split(“/”);
int year=Integer.parseInt(strsplit[0]);
int month=Integer.parseInt(strsplit[1]);
int day=Integer.parseInt(strsplit[2]);
如果((月)检查SimpleDateFormatter.format()
和SimpleDateFormatter.parse()
方法的文档,我已经创建了一个简单的方法来实现这一点。参考mm表示以小时为单位的分钟,mm=以年为单位的月。“yyyy/mm/dd”
,这是OP的要求。
String format = "yyyy/MM/dd";
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse("12/31/2013"); // this date is in format MM/dd/yyyy and it will formatted to the format yyyy/MM/dd ( 2013/12/31 )
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
// formatting
System.out.println(sdf.format(new Date())); // this will format the current date to the format yyyy/MM/dd
private DatePickerDialog.OnDateSetListener mDateSetListene = new DatePickerDialog.OnDateSetListener() {
// onDateSet method
public void onDateSet(DatePicker view, Calendar cal) {
dob.setText(new SimpleDateFormat("yyyy/MM/dd").format(cal.getTime()));
}
};
String strDate="2013/7/7";
String[] strsplit=strDate.split("/");
int year=Integer.parseInt(strsplit[0]);
int month=Integer.parseInt(strsplit[1]);
int day=Integer.parseInt(strsplit[2]);
if((month)<=9)
strDate+="0";
strDate+=(month )+"/";
if(day<=9)
strDate+="0";
strDate+=(day)+"/";
strDate+=(year);