2013-04-22 60 views
2

你好,我要設置日期爲每個用戶選擇的時間AM/PM如何更改日期自動按時間AM/PM

我的時間

private TimePickerDialog.OnTimeSetListener timeListener1 = 
      new TimePickerDialog.OnTimeSetListener() { 

     @Override 
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 


      hours1=hourOfDay; 
      min1=minute; 
      SimpleDateFormat formate = new SimpleDateFormat("HH:mm"); 
      String newTime = formate.format(System.currentTimeMillis()); 


      updateTime1(); 


      Calendar c = Calendar.getInstance(); 
       int seconds = c.get(Calendar.SECOND); 
       int minutes = c.get(Calendar.MINUTE); 
       int hours = c.get(Calendar.HOUR); 
       int years = c.get(Calendar.YEAR); 
       int months = 1 + c.get(Calendar.MONTH); 
       int days = c.get(Calendar.DAY_OF_MONTH); 
       int AM_orPM = c.get(Calendar.AM_PM); 

       try{ 
        if ((c.get(Calendar.AM_PM) == 1)) 
        { 
         String PM = ""; 
         if (AM_orPM == 1) 
         { 
          PM = "PM"; 
         } 
         populateSetDate2(days, months, years); 
         Cabdate.setText("Refreshed on " + days + "-" 
         + months + "-" + years + PM); 

        } 
        else if ((c.get(Calendar.AM_PM) == 0)) 
        { 
         String AM = ""; 
         if (AM_orPM == 0) 
         { 
          AM = "AM"; 
         } 
         populateSetDate2(ddd1, mmm1, ddd1); 

         Cabdate.setText("Refreshed on " + ddd1 + "-" 
         + mmm1 + "-" + yyy1 + AM); 

        } 
       } 
       catch (Exception e){} 
      /* if(hours1>=hours) 
      { 
       hours=hours1+5; 

       Toast.makeText(AirlinecabBooking.this, "invalid",Toast.LENGTH_LONG).show(); 
      }*/ 



     } 

    }; 

對於日期代碼:

@SuppressLint({ "ValidFragment", "NewApi" }) 
public class SelectDateFragment2 extends DialogFragment implements 
OnDateSetListener { 


    @SuppressLint("NewApi") 
    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 
     final Calendar calendar = Calendar.getInstance(); 

     yyy1 = calendar.get(Calendar.YEAR); 
     mmm1 = calendar.get(Calendar.MONTH); 
     ddd1 = calendar.get(Calendar.DAY_OF_MONTH); 
     mmm1= mmm1 + 1; 


     Calendar plus10days = Calendar.getInstance(); 
     plus10days.add(Calendar.DAY_OF_YEAR, 31); 
     d = plus10days.get(calendar.DAY_OF_YEAR); 
      calendar.get(Calendar.AM_PM); 



     int yy = calendar.get(Calendar.YEAR); 
     int mm = calendar.get(Calendar.MONTH); 
     int dd = calendar.get(Calendar.DAY_OF_MONTH); 

     return new DatePickerDialog(getActivity(), this, yy, mm, dd); 
    } 

    @Override 
    public void onDateSet(DatePicker arg0, int yy, int mm, int dd) { 
     // TODO Auto-generated method stub 

      populateSetDate2(yy, mm + 1, dd); 



    } 
} 

public void populateSetDate2(int year, int month, int day) { 






    if (year == yyy1) { 

     if ((month >= mmm1) && (year == yyy1)) { 


      if(day > ddd1 && day < d) 
      { 

       Cabdate.setText(day + "/" + month + "/" + year); 
       final_date=year+"-"+month+"-"+day; 
       System.out.println(final_date); 
      } 
      else 
      { 
       Toast.makeText(getApplicationContext(), 
         "Sorry Problem with date. ", Toast.LENGTH_SHORT) 
         .show(); 
      } 

     } 

     else if (month >= mmm1 && year == yyy1) 
     { 
      Cabdate.setText(day + "/" + month + "/" + year); 
      final_date=year+"-"+month+"-"+day; 
      System.out.println(final_date); 
     } 

     else 
     { 
      Toast.makeText(getApplicationContext(), 
        "Sorry Problem with month. ", Toast.LENGTH_SHORT) 
        .show(); 
     } 

    } 
    else 
    { 
     Toast.makeText(getApplicationContext(), 
       "Sorry Problem with year. ", Toast.LENGTH_SHORT).show(); 


    } 

問題是,當我選擇任何時間格式AM/PM它設置日期作爲當前的日期,即使我選擇未來的日期..

這裏Cabdate是我迄今爲止textview

請幫我 謝謝

回答

0

Calendar.getInstance();返回Calendar例如在默認時區當前時間initilalized。您應該使用setTimeInMillis(long millis)setTime(Date date)來更改Calendar實例中的時間。

當您關閉您的DatePiker

int day = datePicker.getDayOfMonth(); 
int month = datePicker.getMonth(); 
int year = datePicker.getYear(); 

Calendar calendar = Calendar.getInstance(); 
calendar.set(year, month, day); 
+0

plzzz給我解釋一下我在哪裏initilalize setTimeInMillis(長米利斯)M迷惑plz幫助我 – user2306201 2013-04-22 07:32:39

+0

看到我的編輯.... – Blackbelt 2013-04-22 07:35:13

0

使用此:

TimerTask scanTask; 
final Handler handler = new Handler(); 
Timer t = new Timer(); 
scanTask = new TimerTask() { 
    public void run() { 
     handler.post(new Runnable() { 
      public void run() { 
       Calendar c = Calendar.getInstance(); 
       int seconds = c.get(Calendar.SECOND); 
       Date mydate = Calendar.getInstance().getTime(); 
       SimpleDateFormat formatter = new SimpleDateFormat(
         "MMM dd yyyy hh:mm:ss a"); 
       String time = formatter.format(mydate); 
       // Toast.makeText(MainActivity.this, time, 
       // Toast.LENGTH_SHORT).show(); 
       // textExpiry.setText(formatter.format(dateTime.getTime())); 
       mTextTime.setText(time); 

      } 
     }); 
    } 
}; 

t.schedule(scanTask, 1000, 1000); 
+0

我的日期不會按照上午/下午更改PLZZZZZ幫助我 – user2306201 2013-04-22 07:51:04

+0

答案在dateFormat中更新。這會給你AM AM格式的時間。 – URAndroid 2013-04-22 08:06:45

+0

是的,它給我在上午/下午的甲酸,但日期劑量改變,例如,如果我選擇25日4月日期和設置時間....日期自動設置爲當前日期4月22日... :( – user2306201 2013-04-22 10:35:48