在我的应用中,我以特定格式从我的数据库中检索日期。 (由PHP生成)
我想在我的Android应用中针对这种情况显示特定输出:
来自数据库的输入格式: 2014-05-30 17:50:50
我希望能够在TexView中显示这种格式:
今天 - 17h50
昨天 - 17h50
6月5日 - 17日50日
我该怎么做?
[UPDATE]
String dateDebut = annonce.getDate_debut();
SimpleDateFormat inDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // inputFormat
SimpleDateFormat TodayDF = new SimpleDateFormat("HH'h'mm"); //OutputFormat For today and yesterday
SimpleDateFormat FullDF = new SimpleDateFormat("dd MMM - HH'h'mm"); //Outputformat long
Date inDate = null;
try {
inDate = inDF.parse(dateDebut);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//calendar for inputday
Calendar inCal = new GregorianCalendar();
inCal.setTime(inDate);
//startOfToday
Calendar cStartOfDate = new GregorianCalendar();
cStartOfDate.set(Calendar.HOUR, 0);
cStartOfDate.set(Calendar.MINUTE, 0);
cStartOfDate.set(Calendar.SECOND, 0);
cStartOfDate.set(Calendar.MILLISECOND, 0);
//endOfToday
Calendar cEndOfDate = new GregorianCalendar();
cEndOfDate.set(Calendar.HOUR, 23);
cEndOfDate.set(Calendar.MINUTE, 59);
cEndOfDate.set(Calendar.SECOND, 59);
//startOfYesterday
Calendar cStartOfYesterday = new GregorianCalendar();
cStartOfYesterday.set(Calendar.HOUR, 0);
cStartOfYesterday.set(Calendar.MINUTE, 0);
cStartOfYesterday.set(Calendar.SECOND, 0);
cStartOfYesterday.set(Calendar.MILLISECOND, 0);
//endOfYesterday
Calendar cEndOfYesterday = new GregorianCalendar();
cEndOfYesterday.set(Calendar.HOUR, 23);
cEndOfYesterday.set(Calendar.MINUTE, 59);
cEndOfYesterday.set(Calendar.SECOND, 59);
if (cStartOfDate.before(inCal) && cEndOfDate.after(inCal)){
System.out.println("Aujourd'hui - "+TodayDF.format(inDate));
viewHolder.dateDebut.setText("Aujourd'hui - "+TodayDF.format(inDate));
} else if (cStartOfYesterday.before(inCal) && cEndOfYesterday.after(inCal)){
System.out.println("Hier - "+TodayDF.format(inDate));
viewHolder.dateDebut.setText("Hier - "+TodayDF.format(inDate));
} else {
System.out.println(FullDF.format(inDate));
viewHolder.dateDebut.setText(FullDF.format(inDate));
}
答案 0 :(得分:2)
试用此代码:
DateFormat inDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // inputFormat
DateFormat TodayDF = new SimpleDateFormat("HH'h'mm"); //OutputFormat For today and yesterday
DateFormat FullDF = new SimpleDateFormat("dd MMM - HH'h'mm"); //Outputformat long
Date inDate = inDF.parse("2014-06-05 17:50:50");
//calendar for inputday
Calendar inCal = new GregorianCalendar();
inCal.setTime(inDate);
//startOfToday
Calendar cStartOfDate = new GregorianCalendar();
cStartOfDate.set(Calendar.HOUR_OF_DAY, 0);
cStartOfDate.set(Calendar.MINUTE, 0);
cStartOfDate.set(Calendar.SECOND, 0);
cStartOfDate.set(Calendar.MILLISECOND, 0);
//endOfToday
Calendar cEndOfDate = new GregorianCalendar();
cEndOfDate.set(Calendar.HOUR_OF_DAY, 23);
cEndOfDate.set(Calendar.MINUTE, 59);
cEndOfDate.set(Calendar.SECOND, 59);
//startOfYesterday
Calendar cStartOfYesterday = new GregorianCalendar();
cStartOfYesterday.set(Calendar.HOUR_OF_DAY, 0);
cStartOfYesterday.set(Calendar.MINUTE, 0);
cStartOfYesterday.set(Calendar.SECOND, 0);
cStartOfYesterday.set(Calendar.MILLISECOND, 0);
//endOfYesterday
Calendar cEndOfYesterday = new GregorianCalendar();
cEndOfYesterday.set(Calendar.HOUR_OF_DAY, 23);
cEndOfYesterday.set(Calendar.MINUTE, 59);
cEndOfYesterday.set(Calendar.SECOND, 59);
if (cStartOfDate.before(inCal) && cEndOfDate.after(inCal)){
System.out.println("Today "+TodayDF.format(inDate));
} else if (cStartOfYesterday.before(inCal) && cEndOfYesterday.after(inCal)){
System.out.println("Yesterday"+TodayDF.format(inDate));
} else {
System.out.println(FullDF.format(inDate));
}
答案 1 :(得分:0)
代码:
Calendar cal = Calendar.getInstance();
new SimpleDateFormat("dd MMMM - HH").format(cal.getTime())+
"h" + new SimpleDateFormat("mm").format(cal.getTime())
答案 2 :(得分:0)
这是一个完整的解决方案。我没试过,但它应该有效。
NB:请注意输入限制:例如,如果日期是01/01/2015,我不确定它是否会起作用。 我让你测试一下。
private boolean checkSameDate(Calendar cal1, Calendar cal2) {
if ((cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR))
&& (cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR))) {
return true;
}
return false;
}
private void checkDate(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
Calendar cal2 = new GregorianCalendar();
cal2.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, 1);
if (checkSameDate(cal, cal2)) {
// Your input date is tomorrow.
} else {
cal.add(Calendar.DAY_OF_YEAR, -2);
if (checkSameDate(cal, cal2)) {
// Your input date is yesterday.
} else {
DateFormat sdf = new SimpleDateFormat("dd MMMM - HH:mm");
System.out.println(sdf.format(date));
}
}
}
修改强>
抱歉,我认为今天是01/01 / YYYY时,31/12 / YYYY-1的日期不适用。也许您可以使用这种解决方案修复此代码:Check if one date is exactly 24 hours or more after another
对于SimpleDateFormat
,如果这是您需要的格式,我可以在此处https://ideone.com/dsxKN9查看。
修改2
我只是看到你想要今天而不是明天 :)。我的错!我会尝试解决此问题,但如果您理解逻辑,那么您就可以做到。
答案 3 :(得分:0)
您可以使用此功能:
public static String convertDate(String stringDate, String oldFormat) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(oldFormat);
Date date = sdf.parse(stringDate);
double daysAgo = (System.currentTimeMillis() - date.getTime()) / (24 * 60 * 60 * 1000d);
System.out.println(daysAgo);
String newFormat;
if (daysAgo<=0){
newFormat="'Today -' HH'h'mm";
}
else if (daysAgo>=0 && daysAgo<=1){
newFormat="'Yesterday -' HH'h'mm";
}
else {
newFormat="d MMMM '-' HH'h'mm";
}
sdf.applyPattern(newFormat);
return sdf.format(date);
}
用法是:
String newDate = convertDate("2014-06-03 17:50:50", "yyyy-MM-dd HH:mm:ss");