2016-09-17 12 views
-1

この日付と時刻を文字列に連結していますが、それは正しいのですか?ここでは、コード文字列の値をLongに渡す

public void DT(){ 
     seldate = (TextView) findViewById(R.id.receivedate); 
     newDate = seldate.getText().toString(); 
     timeChose = time1.getText().toString(); 
     final TimePicker tp = (TimePicker) findViewById(R.id.timePicker1); 

     strDateTime = newDate + " "+ timeChose ; 
     DatabaseSource sched = new DatabaseSource(
       subject.getText().toString(), 
       description.getText().toString(), 
       strDateTime.toLong() 
     ); 
     long result = dbHelper.addSched(sched); 
     if(result>0){ 
      Toast.makeText(getApplicationContext(),"Added", Toast.LENGTH_SHORT).show(); 
     } 
     else { 
      Toast.makeText(getApplicationContext(),"Add Failed", Toast.LENGTH_SHORT).show(); 
     } 
} 

PS選択した日付です 私は(そのコンストラクタは長いので)ロングタイプにstrDateTimeを渡したいので、私は好みのキーに保存し、それを取得し、中のTextViewに渡します次のアクティビティ(イベントを作成する場所)をクリックしてから、editpextをクリックすると、timepickerdialogがポップアップし、edittextフィールド自体に時刻が設定されます。

+0

、これを試してみてください? –

+0

文字列strDateTime –

+0

「17-09-2016」のような形式ですか?サンプル日付を表示する。 –

答えて

0

Dateオブジェクトにyyyy-MM-dd形式で日付を解析。 getTimeメソッドを使用して、ミリ秒単位でタイムスタンプを取得します。

は `strDateTime`の形式は何

String strDateTime = "2016-09-17 11:21 PM"; 

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm aa"); 
long milliseconds = 0; 
try { 
    Date date = format.parse(strDateTime); 
    milliseconds = date.getTime(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

// pass milliseconds to the method 
DatabaseSource sched = new DatabaseSource(
     subject.getText().toString(), 
     description.getText().toString(), 
     milliseconds 
); 
+0

strDateTimeが時間と連結されているのを忘れていたので、日付と時刻の両方がstrDateTime(文字列)に連結されています。データベースはこのように(2016-09-17 11:21 PM)データベースのDATETIMEデータ型と同じようになります –

+0

したがって、strDateTime.toLong()の行で、これは正しいですか?別のクラスのコンストラクタはこのようなものです(String、String、Long)? –

+0

編集を確認してください。 –

0

使用このコードLong.parseLong("0", 0);

ここ"0"はあなたの文字列値で、0はデフォルト値です(文字列が数値形式で空であるかどうかを包み)

関連する問題