2014-11-17 361 views
1

我正在其中我在這個格式歌廳字符串的應用字符串轉換爲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; 
    } 

} 
+3

什麼是'KK:mm'是代表什麼? – SilentKiller

+0

@SilentKiller kk適用於想要獲得24小時格式的結果。 –

+0

@pratt「k」用於在24小時內獲得小時,「K」用於從am/pm(0-11)小時獲得hout。它不是24小時格式。 – SilentKiller

回答

1

我不明白你爲什麼格式化日期,因爲你已經有小時和分鐘。

你只需要使用這樣來24小時時間格式

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

掌握下面的代碼試試我:

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); 
+0

得到相同的錯誤 – user3917131

+0

@ user3917131它不是因爲這個代碼,請檢查您的代碼是否正確,然後粘貼錯誤的代碼行。 –

+0

我做了同樣的方式bt沒有得到輸出 – user3917131

0

這將工作。我現在就試了一下。

Calendar cal = Calendar.getInstance(); 
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); 
    String dateString = sdf.format(cal.getTime()); 
0

使用此格式的日期:

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)); 
相關問題