2011-08-05 10 views
0

私のアンドロイドアプリケーションでは、日付と時刻をTextViewに設定します。その日付の値で私はStringとして選択して保存しました。今度は、Stringの日付と現在の時刻を設定します。日付時刻の値を設定する方法は?

次のコードを試しました。保存された日付の値が表示されましたが、時刻は午前12時です。現在時刻がほしいです

String lbl_date="02-Aug-2011"; 
Date dateObj = new Date(lbl_date); 
SimpleDateFormat postFormater = new SimpleDateFormat("dd-MMM-yyyy hh:mm aa",Locale.ENGLISH); 
String newDateStr = postFormater.format(dateObj);   
txt_date_start.setText(newDateStr.toString()); 

出力として:02-Aug-2011 12:00 AM

必要があります。02-Aug-2011 [current time]

答えて

2
Date now = new Date(); 

String s; 

Format formatter; 

formatter = new SimpleDateFormat("dd-MMM-yyyy hh:mm aa",Locale.ENGLISH); 

s = formatter.format(date); 

txt_date_start.setText(a); 
2

ただ、現在の時刻を設定し、これを使用

final Calendar c = Calendar.getInstance(); 
int mYear = c.get(Calendar.YEAR); 
int mMonth = c.get(Calendar.MONTH); 
int mDay = c.get(Calendar.DAY_OF_MONTH); 
int mHour = c.get(Calendar.HOUR_OF_DAY); 
int mMinute = c.get(Calendar.MINUTE); 
    TextView t=(TextView)findViewbyid(R.id.text); 
    t.setText(mYear+" "+mMOnth+" "+mDay+" "); 
1

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

Calendar c = Calendar.getInstance(); 
dateObj.setTime(c.getTimeInMillis()); 
+0

現在の日付と現在の時刻が表示されます。しかし、私は現在の時刻をlbl_dateにします – Selva

+0

あなたは何をしようとしているのか分かりにくいです。あなたは現在の時間を持つことができます。 g。 'SimpleDateFormat timeFormat = new SimpleDateFormat(" hh:mm ");これは次のようになります。 String time = timeFormat.format(c.getTimeInMillis()); ' – iDroid

関連する問題