我正在制作一个应用程序,我正在以这种格式创建一个字符串
2014-11-17 13:41:25
但我想要的只是这个
13:41
我尝试了这个,但我得到了关闭
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;
}
}
答案 0 :(得分:2)
尝试使用以下代码:
String date = "2014-11-25 14:30";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd HH:MM");
Date testDate = null;
try {
testDate = sdf.parse(date);
}catch(Exception ex){
ex.printStackTrace();
}
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm");
String newFormat = formatter.format(testDate);
System.out.println(".....Date..."+newFormat);
答案 1 :(得分:2)
我不明白你为什么要格式化日期,因为你已经有了小时和分钟。
您只需使用此功能即可获得24小时TimeFormat
String time = new SimpleDateFormat("HH:mm").format(mCalendar.getTime());
在将数据发送回活动时,您可以使用Handler
尝试使用以下代码
FragmentActivity
MainActivity.java:
@SuppressLint("HandlerLeak")
public class MainActivity extends FragmentActivity {
private int mHour = 0;
private int mMinute = 0;
/** This handles the message send from TimePickerDialogFragment on setting Time */
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message m) {
Bundle b = m.getData();
mHour = b.getInt("set_hour");
mMinute = b.getInt("set_minute");
Toast.makeText(getBaseContext(), b.getString("set_time"), Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Getting an instance of Set button */
Button btnSet = (Button) findViewById(R.id.btnSet);
/** Setting click event listener for the button */
btnSet.setOnClickListener(listener);
}
/** Click Event Handler for button */
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
Bundle b = new Bundle();
Calendar mCalendar = Calendar.getInstance();
b.putInt("set_hour", mCalendar.get(Calendar.HOUR_OF_DAY));
b.putInt("set_minute", mCalendar.get(Calendar.MINUTE));
TimePickerDialogFragment timePicker = new TimePickerDialogFragment(mHandler);
timePicker.setArguments(b);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(timePicker, "time_picker");
ft.commit();
}
};
}
TimePickerDialogFragment.java:
public class TimePickerDialogFragment extends DialogFragment {
private Handler mHandler;
private int mHour;
private int mMinute;
public TimePickerDialogFragment(Handler h) {
mHandler = h;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle b = getArguments();
mHour = b.getInt("set_hour");
mMinute = b.getInt("set_minute");
TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, minute);
String time = new SimpleDateFormat("HH:mm").format(mCalendar.getTime());
Bundle b = new Bundle();
b.putString("set_time", "Set Time : " + time);
Message m = new Message();
m.setData(b);
mHandler.sendMessage(m);
}
};
return new TimePickerDialog(getActivity(), listener, mHour, mMinute, false);
}
}
答案 2 :(得分:1)
使用它来格式化日期:
String startTime = "2014-11-17 13:41:25";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = dateFormat.parse(startTime);
long time = date1.getTime();
dateFormat.applyPattern("HH:mm");
System.out.println(dateFormat.format(time));
答案 3 :(得分:0)
这会奏效。我现在就试过了。
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String dateString = sdf.format(cal.getTime());