Right way to format date with strings like today, yesterday, tomorrow etc

前端 未结 6 551
臣服心动
臣服心动 2021-02-04 00:31

I have a date textview. And my textview holds a date string like 2011.09.17. Well I still do want to have that but I also want to add some more user friendly info for some speci

6条回答
  •  梦如初夏
    2021-02-04 00:58

    Try this, i implemented it using joda-datatime2.2.jar and java SimpleDateFormat

    import java.text.SimpleDateFormat;
    import java.util.Date;
    import org.joda.time.DateMidnight;
    import org.joda.time.DateTime;
    import org.joda.time.Days;
    public class SmartDateTimeUtil {
    private static String getHourMinuteString(Date date){
        SimpleDateFormat hourMinuteFormat = new SimpleDateFormat(" h:m a");
        return hourMinuteFormat.format(date);
    }
    
    private static String getDateString(Date date){
        SimpleDateFormat dateStringFormat = new SimpleDateFormat("EEE',' MMM d y',' h:m a");
        return dateStringFormat.format(date);
    }
    
    private static boolean isToday (DateTime dateTime) {
           DateMidnight today = new DateMidnight();
           return today.equals(dateTime.toDateMidnight());
    }
    
    private static boolean isYesterday (DateTime dateTime) {
           DateMidnight yesterday = (new DateMidnight()).minusDays(1);
           return yesterday.equals(dateTime.toDateMidnight());
    }
    
    private static boolean isTomorrow(DateTime dateTime){
        DateMidnight tomorrow = (new DateMidnight()).plusDays(1);
           return tomorrow.equals(dateTime.toDateMidnight());
    }
    private static String getDayString(Date date) {
            SimpleDateFormat weekdayFormat = new SimpleDateFormat("EEE',' h:m a");
            String s;
            if (isToday(new DateTime(date)))
                s = "Today";
            else if (isYesterday(new DateTime(date)))
                s = "Yesterday," + getHourMinuteString(date);
            else if(isTomorrow(new DateTime(date)))
                s = "Tomorrow," +getHourMinuteString(date);
            else
                s = weekdayFormat.format(date);
            return s;
    }
    
    public static String getDateString_shortAndSmart(Date date) {
            String s;
            DateTime nowDT = new DateTime();
            DateTime dateDT = new DateTime(date);
            int days = Days.daysBetween(dateDT, nowDT).getDays();   
            if (isToday(new DateTime(date)))
                s = "Today,"+getHourMinuteString(date);
            else if (days < 7)
                s = getDayString(date);
            else
                s = getDateString(date);
            return s;
    }
    
    }
    

    Simple cases to use and test the Util class:

    import java.util.Calendar;
    import java.util.Date;
    
    public class SmartDateTimeUtilTest {
        public static void main(String[] args) {
            System.out.println("Date now:"+SmartDateTimeUtil.getDateString_shortAndSmart(new Date()));
            System.out.println("Date 5 days before :"+SmartDateTimeUtil.getDateString_shortAndSmart(getFutureDay(-5)));
            System.out.println("Date 1 day before :"+SmartDateTimeUtil.getDateString_shortAndSmart(getFutureDay(-1)));
            System.out.println("Date last month:"+SmartDateTimeUtil.getDateString_shortAndSmart(getFutureMonth(-1)));
            System.out.println("Date last year:"+SmartDateTimeUtil.getDateString_shortAndSmart(getFutureDate(-1)));
            System.out.println("Date 1 day after :"+SmartDateTimeUtil.getDateString_shortAndSmart(getFutureDay(1)));
        }
        public static Date getFutureDate(int numberOfYears){
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.YEAR, numberOfYears); 
            return c.getTime();
        }
        public static Date getFutureMonth(int numberOfYears){
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MONTH, numberOfYears); 
            return c.getTime();
        }
    
        public static Date getFutureDay(int numberOfYears){
            Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, numberOfYears); 
            return c.getTime();
        }
    }
    

提交回复
热议问题