我使用Calendar类设置日期,并使用SimpleDateFormat设置格式。输出不符合预期。
Input given = 30-9-2018 14:24
Expected output = 30 Sep 2018 Sun 2 : 24 PM
Returned output = 01 Oct 2018 Mon 02 : 24 AM
称为dateTimeFormat(30,9,2018,14,24,"dd MMM yyyy EEE hh : mm a");
下面是我的代码
public static String dateTimeFormat(int date,int month,int year,int curr_hour,int curr_min,String format){
Log.e("Given Date",date+"-"+month+"-"+year+" "+curr_hour+":"+curr_min);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR,year);
cal.set(Calendar.MONTH,month-1);
cal.set(Calendar.DATE,date);
cal.set(Calendar.HOUR,curr_hour);
cal.set(Calendar.MINUTE,curr_min);
SimpleDateFormat format1 = new SimpleDateFormat(format);
String dateF = format1.format(cal.getTime());
return dateF;
}