wawanopoulos
wawanopoulos

Reputation: 9802

Java : How to parse date format to show specific output format?

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 :

Today - 17h50

Yesterday - 17h50

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

Upvotes: 4

Views: 1247

Answers (4)

Jens
Jens

Reputation: 69460

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

Upvotes: 2

Genzotto
Genzotto

Reputation: 1974

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

Upvotes: 0

lpratlong
lpratlong

Reputation: 1461

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.

Upvotes: 0

Dinal
Dinal

Reputation: 661

  • 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())

Upvotes: 0

Related Questions