1,在程序中写个返回值为String类型的静态方法:返回值为规范格式的时间字符串
public static String getDate() {
Calendar ca = Calendar.getInstance();
int year = ca.get(Calendar.YEAR);//获取年份
int month=ca.get(Calendar.MONTH)+1;//获取月份
int day=ca.get(Calendar.DATE);//获取日
int hour=ca.get(Calendar.HOUR_OF_DAY);//小时
int minute=ca.get(Calendar.MINUTE);//分;
int second=ca.get(Calendar.SECOND);//秒
String time;
if(minute<10&&second<10){
time = year+"-"+month+"-"+day+" "+hour+":"+"0"+minute+":"+"0"+second;
}else if(minute<10&&second>=10){
time = year+"-"+month+"-" +day+" "+hour+":"+"0"+minute+":"+second;
}else if(minute>=10&&second<10){
time = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+"0"+second;
}else{
time = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
}