2017-08-03 153 views
0

如何顯示我的列表視圖與下列日期格式,如果它的今天日期只顯示時間,昨天,星期二,星期日,星期日,星期五,星期四,星期三,星期三和星期五開始的時間,星期四,星期二......等等。我如何重複我的日期格式顯示在今天,昨天,星期二,星期一,星期日,太陽,星期三,...星期三,

我試着用這個代碼,但我沒有得到

if(Constant.hours-Constant.hours1 <= 23){ 
        holder.txtTime.setText(newDateString); 
       }else if(Constant.hours-Constant.hours1 >= 23 && Constant.hours-Constant.hours1 <= 47){ 
        holder.txtTime.setText("Yesterday"); 
       }else if(Constant.days >= 2 && Constant.days<=2){ 
        holder.txtTime.setText(Constant.strLong+" days ago"); 
       } 
+0

你究竟想要什麼?你想在幾天前顯示類型信息? –

+0

根據應用程序的需求,我需要這樣展示。 –

+0

使用https://developer.android.com/reference/android/text/format/DateUtils.html#getRelativeTimeSpanString(long,long,long) –

回答

0

試試這個... // 假設您的日期時間格式是可變newDateString的「2017年12月8日」,那麼日期格式將像SimpleDateFormat formatter = SimpleDateFormat .forPattern(「dd-MM-yyyy」); //

String newDateString="08/02/2017 18:41:11"; 
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss"); 
     //SimpleDateFormat formatter = new SimpleDateFormat("your farmat here"); 
       Date yourDate= null; 
       Date current= null; 
       Calendar cal = Calendar.getInstance(); 


    try { 
      yourDate= formatter .parse(newDateString);//newDateString is your variable 
        current= formatter .parse(formatter.format(cal.getTime())); 

       } catch (java.text.ParseException e) { 
        e.printStackTrace(); 
       } 

       int daysDiff= (int) ((yourDate.getTime() - current.getTime())/ (1000 * 60 * 60 * 24)); 

       if(daysDiff< 1){ 
           holder.txtTime.setText(newDateString); 
          }else if(daysDiff== 1){ 
           holder.txtTime.setText("Yesterday"); 
          }else if(daysDiff> 1){ 
           holder.txtTime.setText(daysDiff+" days ago"); 
          } 
          }else if(daysDiff<0){ 
           holder.txtTime.setText(daysDiff+" days before"); 
          } 
+0

08/02/2017 18:41:11這是我的日期格式我需要顯示結果 –

+0

String newDateString = 08/02/2017 18:41:11; SimpleDateFormat formatter = new SimpleDateFormat(「dd/MM/yyyy hh:mm:ss」); 使用這個,你將得到你的答案與提到的解決方案只是做了這個變化 –

+0

newDateString在這個我需要發送? –

相關問題