编程语言
首页 > 编程语言> > Java:如何解析日期格式以显示特定的输出格式?

Java:如何解析日期格式以显示特定的输出格式?

作者:互联网

在我的应用程序中,我以特定格式从我的数据库中检索日期. (由PHP生成)

我想在我的Android应用中针对这种情况显示特定输出:

数据库输入格式:2014-05-30 17:50:50

我希望能够在TexView中显示这种格式:

>如果日期是指今天,我想显示这种格式:

今天 – 17点50分

>如果日期是指昨天,我会显示以下格式:

昨天 – 17点50分

>其他日子:

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));
            }

解决方法:

试试这个代码:

  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));
    }

标签:java,date,android,android-date
来源: https://codeday.me/bug/20190703/1364415.html