我正在其中我在這個格式歌廳字符串的應用字符串轉換爲HH:mm格式
2014-11-17 13:41:25
但我想要的不僅是
13:41
我想幹什麼此但我得到forcse接近
String test = new SimpleDateFormat("kk:mm").format(data.getStringExtra("TIME"));
etStartTime.setText(test);
logcat的:
11-17 14:44:07.717: E/AndroidRuntime(14287): FATAL EXCEPTION: main
11-17 14:44:07.717: E/AndroidRuntime(14287): java.lang.IllegalArgumentException: Bad class: class java.lang.String
11-17 14:44:07.717: E/AndroidRuntime(14287): at java.text.DateFormat.format(DateFormat.java:359)
11-17 14:44:07.717: E/AndroidRuntime(14287): at java.text.Format.format(Format.java:93)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.megasys.im3_new.workorder.TimeCard.onActivityResult(TimeCard.java:318)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.megasys.im3_new.workorder.TimeCard$TimePickerFragment.onTimeSet(TimeCard.java:383)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:130)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.app.TimePickerDialog.onClick(TimePickerDialog.java:115)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:169)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.os.Handler.dispatchMessage(Handler.java:107)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.os.Looper.loop(Looper.java:194)
11-17 14:44:07.717: E/AndroidRuntime(14287): at android.app.ActivityThread.main(ActivityThread.java:5370)
11-17 14:44:07.717: E/AndroidRuntime(14287): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 14:44:07.717: E/AndroidRuntime(14287): at java.lang.reflect.Method.invoke(Method.java:525)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-17 14:44:07.717: E/AndroidRuntime(14287): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-17 14:44:07.717: E/AndroidRuntime(14287): at dalvik.system.NativeStart.main(Native Method)
我做了什麼
try {
String time = data.getStringExtra("TIME");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date timeCompare;
timeCompare = sdf.parse(time);
Date currentTime = new Date();
int comareResult = timeCompare.compareTo(currentTime);
if (comareResult == -1) {
// time = new SimpleDateFormat("HH:mm").format(time);
Date testDate = null;
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-mm-dd HH:MM");
try {
testDate = sdf.parse(time);
} catch(Exception ex) {
ex.printStackTrace();
}
etStartTime.setText("" + testDate);
}
} catch(Exception ex) {
ex.printStackTrace();
}
這是我做了什麼,但還是會收到錯誤。
的時間,我從意圖
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
public int hour;
public int min;
public String time;
Calendar c;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
min = c.get(Calendar.MINUTE);
// Create a new instance of DatePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, min, true);
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar date = Calendar.getInstance();
date.set(Calendar.HOUR_OF_DAY, hourOfDay);
date.set(Calendar.MINUTE, minute);
date.set(Calendar.AM_PM, date.get(Calendar.AM_PM));
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date.getTime());
Intent intent = new Intent();
intent.putExtra("TIME", time);
getTargetFragment().onActivityResult(getTargetRequestCode(), 1, intent);
getDialog().hide();
}
public String onTimeSelected() {
return time;
}
}
什麼是'KK:mm'是代表什麼? – SilentKiller
@SilentKiller kk適用於想要獲得24小時格式的結果。 –
@pratt「k」用於在24小時內獲得小時,「K」用於從am/pm(0-11)小時獲得hout。它不是24小時格式。 – SilentKiller