当前位置: 正能量 » 代码编程 » Java » JAVA转化日期为人性化方式显示

JAVA转化日期为人性化方式显示

<pre class="brush:java;toolbar:false">package net.mmgg.mvc.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**   
 *  描述:转化日期为人性化方式显示    
 * 创建人:吴云辉
 * 创建时间:2015年10月21日 下午4:31:40   
 * 修改人:
 * 修改时间:2015年10月21日 下午4:31:40   
 * 修改备注:   
 * @version   V3.0    
 */

public class RelativeDateFormat {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar cal=Calendar.getInstance();
        cal.setTime(new Date());
        for (int i = 0; i > -15000; i--) {
            cal.add(Calendar.SECOND, i);
            System.out.print(format.format(cal.getTime())+"=");
            System.out.println(format(cal.getTime()));
        }

        //System.out.println(format(format.parse("2015-10-14 00:17:44")));
    }

    public static String format(Date time) throws ParseException{
        if(CalendarTime.YEAR.compare(time)){
            return CalendarTime.YEAR.format(time);
        }
        if(CalendarTime.MONTH.compare(time)){
            return CalendarTime.MONTH.format(time);
        }
        if(CalendarTime.WEEK_OF_MONTH.compare(time)){
            return CalendarTime.WEEK_OF_MONTH.format(time);
        }
        if(CalendarTime.DAY_OF_MONTH.compare(time)){
            return CalendarTime.DAY_OF_MONTH.format(time);
        }
        if(CalendarTime.HOUR.compare(time)){
            return CalendarTime.HOUR.format(time);
        }
        if(CalendarTime.MINUTE.compare(time)){
            return CalendarTime.MINUTE.format(time);
        }
        if(CalendarTime.SECOND.compare(time)){
            return CalendarTime.SECOND.format(time);
        }
        return "刚刚";
    }

}
enum CalendarTime{
    YEAR(Calendar.YEAR,"年前"),
    MONTH(Calendar.MONTH,"个月前"),
    WEEK_OF_MONTH(Calendar.WEEK_OF_MONTH,"周前"),
    DAY_OF_MONTH(Calendar.DAY_OF_MONTH,"天前"),
    HOUR(Calendar.HOUR_OF_DAY,"小时前"),
    MINUTE(Calendar.MINUTE,"分钟前"),
    SECOND(Calendar.SECOND,"秒前");

    int x;
    String y;

    CalendarTime(int x,String y){
        this.x=x;
        this.y=y;
    }

    boolean compare(Date date){
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        Calendar now=Calendar.getInstance();
        now.setTime(new Date());
        return now.get(this.x)!=cal.get(this.x);
    }

    String format(Date date) throws ParseException{
        Calendar nowCal=Calendar.getInstance();
        nowCal.setTime(new Date());
        Calendar dateCal=Calendar.getInstance();
        dateCal.setTime(date);
        String str=(nowCal.get(this.x)-dateCal.get(this.x))+this.y;
        switch (this) {
        case YEAR:
            if(nowCal.get(this.x)==dateCal.get(this.x)+1){
                str= "去年"+(dateCal.get(Calendar.MONTH)+1)+"月";
            }
            if(nowCal.get(this.x)==dateCal.get(this.x)+2){
                str= "前年"+(dateCal.get(Calendar.MONTH)+1)+"月";
            }
            break;
        case MONTH:
            if(nowCal.get(this.x)==dateCal.get(this.x)+1){
                str= "上个月"+(dateCal.get(Calendar.DAY_OF_MONTH))+"号";
            }
            break;
        case WEEK_OF_MONTH:
            if(nowCal.get(this.x)==dateCal.get(this.x)+1){
                str= "上周"+((dateCal.get(Calendar.DAY_OF_WEEK)-1)==0?"日":(dateCal.get(Calendar.DAY_OF_WEEK)-1));
            }
            break;
        case DAY_OF_MONTH:
            if(nowCal.get(this.x)==dateCal.get(this.x)+1){
                str= "昨天"+(dateCal.get(Calendar.HOUR_OF_DAY))+"点";
            }
            if(nowCal.get(this.x)==dateCal.get(this.x)+2){
                str= "前天";
            }
            break;
        default:
            break;
        }
        return str;
    }
}
未经允许不得转载:正能量 » JAVA转化日期为人性化方式显示

相关文章

评论 (0)

1 + 4 =