2012-03-02 212 views
-2

我正在開發一個項目,其中需要根據當前時間更改預訂訂單的時間。我需要做的是我想獲得當前時間,並且必須生成時間字符串列表並將其填充到微調器中。例如。我的開始和結束時間是上午11點到下午2點,目前時間是下午12點30分。所以我需要把目前的時間和生成一個30分鐘的時間間隔列表視圖直到結束時間。所以那將是1:00,1:30,2:00。日期和時間格式

public void doWork() { 
runOnUiThread(new Runnable() { 
    public void run() { 
     try{ 
      TextView txtCurrentTime= (TextView)findViewById(R.id.lbltime); 
       Date dt = new Date(); 
       int hours = dt.getHours(); 
       int minutes = dt.getMinutes(); 
       int seconds = dt.getSeconds(); 
       String curTime = hours + ":"+minute + ":"+ seconds; 
       txtCurrentTime.setText(curTime); 
     }catch (Exception e) {} 
    } 
}); 
} 

class CountDownRunner implements Runnable{ 
// @Override 
public void run() { 
     while(!Thread.currentThread().isInterrupted()){ 
      try { 
      doWork(); 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
        Thread.currentThread().interrupt(); 
      }catch(Exception e){ 
      } 
     } 
} 
} 

我已經得到了這段代碼所以我可以用它來獲得當前小時和分鐘,以hh:mm格式。如何進一步處理?

回答

1

試試這個代碼:

List<String>timeslot=new ArrayList<String>(); 


    String stoptime="04:00 PM"; 
    String starttime="11:00 AM"; 
    SimpleDateFormat df1=new SimpleDateFormat("hh:mm a"); 
    Date d1=null; 
    Date d2=null; 
    try 
    { 
     d1=df1.parse(stoptime); 
     d2=df1.parse(starttime); 
    } 
    catch (ParseException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    Calendar now = Calendar.getInstance(); 

    SimpleDateFormat df2=new SimpleDateFormat("kk"); 
    String curtime=df2.format(now.getTime()); 
    stoptime=df2.format(d1); 
    starttime=df2.format(d2); 
    tv.setText(df2.format(d1)); 
    tv2.setText(curtime); 
    int t1=Integer.parseInt(stoptime); 
    int t2=Integer.parseInt(curtime); 
    int t3=Integer.parseInt(starttime); 
    if(t2>=t3) 
    { 

    while(t2<t1) 
    { 
     SimpleDateFormat df4=new SimpleDateFormat("mm"); 
     String curmin=df4.format(now.getTime()); 
     String stopmin=df4.format(d1); 
     int t4=Integer.parseInt(curmin); 
     if(t4<60) 
     { 
      int t5=60-t4; 
      if(t5>30) 
      { 
       t5=t5-30; 
      } 
      now.add(Calendar.MINUTE, t5); 
      SimpleDateFormat df3=new SimpleDateFormat("hh:mm aa"); 
      curtime=df3.format(now.getTime()); 
      timeslot.add(curtime); 

     } 
     now.add(Calendar.MINUTE, 30); 
     SimpleDateFormat df3=new SimpleDateFormat("hh:mm aa"); 
     curtime=df3.format(now.getTime()); 
     timeslot.add(curtime); 
     curtime=df2.format(now.getTime()); 
     t2=Integer.parseInt(curtime); 
    } 
    tv3.setText(curtime); 
    ArrayAdapter<String>aa=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, timeslot); 
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    timespin.setAdapter(aa); 
    } 
+0

是完美的。我正在尋找的那個。 thankx – samir 2012-03-21 10:28:32

2

我已經爲您的問題創建了一個代碼邏輯,可能是它沒有完全實現,但接近您的問題。 首先我帶了日曆實例,然後開始一個循環來檢查當前時間是否大於14,即2:00 PM,然後循環將繼續生成時間並增加30分鐘,直到達到下午2:00爲止

Calendar c1 =Calendar.getInstance(); 
     String time=""+c1.getTime(); 
     System.out.println("time="+time); 
     System.out.println("hr="+Calendar.HOUR_OF_DAY+"min="+Calendar.MINUTE+"sec="+Calendar.SECOND+"am="+Calendar.AM_PM); 
     while(c1.get(Calendar.HOUR_OF_DAY)<=14) 
     { 
      String hr=""+c1.get(Calendar.HOUR); 
      String mi=""+c1.get(Calendar.MINUTE); 
      String se=""+c1.get(Calendar.SECOND); 
      String curTime = hr + ":"+mi + ":"+ se; 
      System.out.println("time="+curTime); 
      c1.add(Calendar.MINUTE, 30); 
     } 

    } 
+0

非常感謝。但是你可以解釋這一點。我想你把查詢中的數字作爲硬編碼的一個? – samir 2012-03-02 08:17:13

+0

okie第一次我帶caledar實例,然後開始一個循環來檢查當前時間是否大於2:00 PM,然後循環將繼續增加時間並增加30分鐘,直到到達下午2:00 – Maneesh 2012-03-02 08:28:02

+0

需要你的幫助frnd如果可能的話,請發郵件給我。 – samir 2012-03-02 11:19:25

0

爲什麼不使用SimpleDateFormat?

new SimpleDateFormat("HH:mm:ss zzz").format(new Date(c1.getTime())); 
+0

是的,我知道這個,但它不夠花花公子。需要進一步研究 – samir 2012-03-02 08:28:37

+0

如果您只需獲取給定Calendar或Date或Date中的小時值或分鐘值(以Epoch爲單位,以毫秒爲單位),SimpleDateFormat負責處理它,則無需重做所有代碼。 – 2012-03-02 12:13:52

1

如果要顯示在應用程序中使用當前時間數字時鐘它是在貨盤>時間&日期 - >數字時鐘它是一個文本視圖隨時間變化continuously.No需要使用定時器或線程爲主要問題that.answer:爲此,您可以手動使用Timer或Thread.Check小時和分鐘,只是做一件事情來提高你的程序的效率。將布爾變量作爲標誌設置值爲false.at開始程序檢查每分鐘後的小時和分鐘。獲得第一個標誌後,將該標誌設置爲True,然後將該線程或計時器休眠到30分鐘,您將獲得更好的結果。