4

In my app, i retrieve date from my database in a specific format. (Generated by PHP)

I would like to show a specific output in my Android app for this cases :

Input format from database : 2014-05-30 17:50:50

I would like to be able to show this format in a TexView :

  • if the date refers to today, i would like to show this format :

Today - 17h50

  • if the date refers to yesterday, i would to show this format :

Yesterday - 17h50

  • And for others days :

5 June - 17h50

How can i do that ?

[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));
            }
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166
  • 2
    What did you try? You should use `DateFormat` (with `SimpleDateFormat` implementation) for the `5 june - 17h50` format: http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html . For the `Today / Yesterday`, you can use `Calendar` (`GregorianCalendar` implementation). – lpratlong Jun 05 '14 at 09:33
  • possible duplicate of [How to calculate "time ago" in Java?](http://stackoverflow.com/questions/3859288/how-to-calculate-time-ago-in-java) – Basil Bourque Jun 05 '14 at 09:43

4 Answers4

2

Try out this Code:

  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));
    }
Jens
  • 67,715
  • 15
  • 98
  • 113
0
  • First convert the date obtained from database to a Calendar instance
  • Today and Yesterday can be identified with the Calendar instance
  • For the other formats use below:

Code:

Calendar cal = Calendar.getInstance();
new SimpleDateFormat("dd MMMM - HH").format(cal.getTime())+ 
"h" + new SimpleDateFormat("mm").format(cal.getTime())
Dinal
  • 661
  • 4
  • 9
0

Here is a complete solution. I did not try it, but it should work.

NB: Be carefull about input limits: I am not sure it will work if the date is 01/01/2015 for example. I let you test this.

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

Edit

Sorry, I think it will not work for a date in 31/12/YYYY-1 when today is 01/01/YYYY. Maybe you can fix this code with this kind of solution: Check if one date is exactly 24 hours or more after another

For SimpleDateFormat, I let you check here https://ideone.com/dsxKN9 if this is the format you need.

Edit 2

I just see that you want today and not tomorrow :). My bad! I'll try fix this, but if you understand the logical, you'll be able to do it.

Community
  • 1
  • 1
lpratlong
  • 1,421
  • 9
  • 17
0

You can do it in using this function:

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

And the usage would be:

String newDate = convertDate("2014-06-03 17:50:50", "yyyy-MM-dd HH:mm:ss");
Genzotto
  • 1,954
  • 6
  • 26
  • 45