Simplify replacement of date object with “today” and “yesterday” strings in Java static method

后端 未结 9 2219
鱼传尺愫
鱼传尺愫 2020-12-30 05:55

I have following method that I would like to make shorter or faster if nothing else. Please all comments are welcome:

Bellow method takes a date object, formates i

9条回答
  •  忘掉有多难
    2020-12-30 06:14

    my understanding of the question is provide a simple method to produce output like the following:

    Today at 20:00
    Today at 20:30
    Today at 21:00
    Tomorrow at 06:45
    Tomorrow at 07:00
    Tomorrow at 08:15
    

    the code below worked for me, but i am new to android and maybe others could point out if the code is not robust. in code below 'timeLong' is the time of my events in epoch time (milliseconds).

    public String convertFromEpochTime (long timeLong) {
        long timeNow = System.currentTimeMillis();
    
        // get day in relative time
        CharSequence timeDayRelative;
        timeDayRelative = DateUtils.getRelativeTimeSpanString(timeLong, timeNow, DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);
    
        // get hour in 24 hour time
        Format hourFormatter = new SimpleDateFormat("HH:mm");
        String timeHour = hourFormatter.format(timeLong);
    
        // Log.d(DEBUG_TAG, "time of event: " + timeDayRelative + " at " + timeHour);
    
        String timeDayHour = timeDayRelative + " at "+ timeHour;
    
        return timeDayHour;
    }
    

提交回复
热议问题