如何以下列日期格式显示我的列表视图,例如,如果今天的日期只显示时间,昨天,星期二,星期一,星期日,星期五,星期五,星期四,星期日和星期五开始时间,星期日,星期二。 .....等等?
我尝试使用此代码,但我没有得到
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 :(得分:1)
试试这个...... // 假设您的日期时间格式为&#34; 12-08-2017&#34;变量newDateString然后日期格式将类似于SimpleDateFormat formatter = SimpleDateFormat .forPattern(&#34; dd-MM-yyyy&#34;); //
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");
}