-2

私は、時間が 11:26 のときに 11:07 を示しているアプリケーションを開発しています。Calendar インスタンスを使用して行いました。

Calendar currentDate=Calendar.getInstance();
        SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
        datenow=ddMMyyyy.format(currentDate.getTime());

        if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
            timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MONTH)+":PM";
        }else{
            timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MONTH)+":AM";
        }
        new MyToast(this, "Date = "+datenow+" time = "+timenow);

出力が間違っています。どうすればよいですか?

4

2 に答える 2

0

monthの代わりに表示していますminute。あなたのコードを私のコードに変更してください

Calendar currentDate=Calendar.getInstance();
SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
String datenow = ddMMyyyy.format(currentDate.getTime());

String timenow;
if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
    timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":PM";
}else{
    timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":AM";
}
Toast.makeText(MainActivity.this, "Date = "+datenow+" time = "+timenow,Toast.LENGTH_SHORT).show();

07==>年の月数であるため11:07が表示されています。月は0->11から計算されます

于 2012-08-07T09:10:05.590 に答える
0

ここを間違えましたCalendar.HOUR++ :Calendar.MONTH次のようになります、

Calendar currentDate=Calendar.getInstance();
        SimpleDateFormat ddMMyyyy=new SimpleDateFormat("dd/MM/yyyy");
        datenow=ddMMyyyy.format(currentDate.getTime());

        if(currentDate.get(Calendar.AM_PM)==Calendar.PM){
            timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":PM";
        }else{
            timenow=currentDate.get(Calendar.HOUR)+":"+currentDate.get(Calendar.MINUTE)+":AM";
        }
        new MyToast(this, "Date = "+datenow+" time = "+timenow);

MINUTEの代わりに、CalendarClassのMONTHオブジェクトにアクセスしていました。

于 2012-08-07T09:11:51.497 に答える