日期:2014-05-17  浏览次数:20753 次

为了过中秋节,大家一起来手写一个java日期处理类~~~~~~
不好意思,标题党了一下~~~~
我的问题是这样的,我昨天没说清楚。这样啊,我来把大框架说一说,大家一起来填空哈。。。
我的目标是写一个java日期处理类,就像新浪微博那样的。功能是实现两个方法,一个是我在保存条目的时候要记录
一下当前的时间,不管什么格式都行,因为只是记录,不是显示。另一个方法是我在要显示时间的时候把原来存进去的时间
拿进来转化一下,更当前时间比较一下,如果是同年,就不显示年份,如果是同月,就不显示月份,如果是当天就显示今天,
如果是昨天就显示昨天,如果是半个小时以前,就显示 xx分钟以前,其他的就显示 xx年xx月xx日 HH:MM:SS 。
下面是部分代码:
Java code

public class DateHandler {
  public String getNewDate(){//这里不一定返回String
     //........
     return //返回一个用于记录的当前时间,别的方法调用就能的到此数据,任何格式都行,只是用于记录储存。
  }
  
  public String handelDate(//传进来原来储存的数据 aa){
     //这里再获得一个当前的日期 bb,
     //这里做比较,如果 aa的年份与 bb 相同,去掉年份,月份相同去掉月份,如果是当天就显示今天,
     //如果是昨天就显示昨天,如果是半个小时以前,就显示 xx分钟以前,其他的就显示 xx年xx月xx日 HH:MM:SS 。
    return //返回处理过后的字符串。用于前台显示。
  }
}



基本上我就这么个意思,想要实现这么个东东,但是我对各种数据类型,尤其是date类的,特别不是很懂,不知道歌会总数据的转化和处理,所以,像求求大神们,能不能集思广益一下,小弟就当是学习。
祝有心的大神中秋遇美女,国庆中大奖,各种好人有好报。

------解决方案--------------------
以前写的一个,参考一下:
Java code
public class DateManage {

    public String getCurrentDate(){
        
        Date date = new Date();
        
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        return sf.format(date);
    
    }
      
    public String manageDate(String arg_date){
        
        String return_str = null;
        
        Date current_time = new Date();
        
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        Date arg_time = null;
        
        try {
            
            arg_time = df.parse(arg_date);
            
        } catch (ParseException e) {
            
            e.printStackTrace();
            
        }
        
        if(current_time.getYear() == arg_time.getYear()) {
            
            SimpleDateFormat delyear = new SimpleDateFormat("MM月dd日 HH:mm:ss");
        
            return_str = delyear.format(arg_time);
            
            if(current_time.getMonth() == arg_time.getMonth()) {
                
                SimpleDateFormat delmonth = new SimpleDateFormat("dd日 HH:mm:ss");
            
                return_str = delmonth.format(arg_time);
                
                if(current_time.getDate() == arg_time.getDate()) {
                    
                    SimpleDateFormat deldate = new SimpleDateFormat("HH:mm:ss");
                
                    return_str = "今天 " + deldate.format(arg_time);
                    
                    if(current_time.getHours() == arg_time.getHours() && 30 > current_time.getMinutes()-arg_time.getMinutes()) {
                        
                        return_str = current_time.getMinutes()-arg_time.getMinutes() + "分钟前";
                        
                        if(0 == current_time.getMinutes()-arg_time.getMinutes()) {
                            
                            return_str = current_time.getSeconds()-arg_time.getSeconds() + "秒前";
                            
                        }
                        
                    }
                    
                } else if(1 == current_time.getDate() - arg_time.getDate()) {
                
                    SimpleDateFormat deldate = new SimpleDateFormat("HH:mm:ss");
                
                    return_str = "昨天 " + deldate.format(arg_time);
                    
                }
                
            }
            
        } else {
            
            SimpleDateFormat alldate = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            
            return_str = alldate.format(arg_time);
            
        }
        
        return return_str;