欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java基础-Date和SimpleDateFormat

程序员文章站 2024-03-15 19:24:24
...

Java基础-Date和SimpleDateFormat

public class DateTools {

public static final String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";  

/**
 * 日期转为格式化的字符串
 * @param time
 * @param format
 * @return String
 */
public static String formatDateToString(Date time,String format){
    SimpleDateFormat sdf = new SimpleDateFormat(format);

    return sdf.format(time);
}

/**
 * 按照制定格式format将date转换为相应的时间格式
 * @param date
 * @param format
 * @return Date
 */
public static Date formatStringToDate(String date,String format){
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    try {
        return sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        throw new RuntimeException(e.toString()); 
    }
}

/**
 * 获取系统时间的毫秒值
 * @param format
 * @return String
 */
public static long getNowTimeMills(){
    Date day=new Date();    
    return day.getTime();
}

/**
 * 获取某时间的毫秒值
 * @param date
 * @param format
 * @return long
 */
public static long getTimeMills(String date,String format){
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date day;
    try {
        day = sdf.parse(date);
    } catch (ParseException e) {
        e.printStackTrace();
        throw new RuntimeException(e.toString()); 
    }
    return day.getTime();
}

/**
 * 时间差
 * @param t1
 * @param t2
 * @return long
 */
public static long compareTime(long t1,long t2){
    if(t1 == t2){
        return 0;
    }else {
        return t1-t2;
    }
}

/**
 * 根据时间差得到发布日期信息
 * @param result
 * @return String
 */
public static String getResult(long result){

    if(result == 0){
        return "刚刚发布";
    }else if(result > 0){
        long time = result/(1000*3600);
        //System.out.println(time);
        if(time < 1){
            return "刚刚发布";
        }else if(time < 24){
            return time+"小时前发布";
        }else if(time < 148){
            return time/24+"天前发布";
        }else{
            return "1";
        }
    }else if(result < 0){
        return "";
    }
    return "";
}

}
—————–测试———————
public class Test {

public static void main(String[] args) {
    Product[] pro = new Product[5];
    pro[0] = new Product("1001","桃","2018-04-18 00:30:01");
    pro[1] = new Product("1002","西瓜","2018-04-17 19:30:01");
    pro[2] = new Product("1003","橙子","2018-04-17 09:30:01");
    pro[3] = new Product("1004","梨","2018-03-17 09:30:01");
    pro[4] = new Product("1005","苹果","2018-04-15 09:30:01");
    Date day;
    for(int i = 0; i < pro.length; i++){
        String time = pro[i].getUploadTime();
        day = DateTools.formatStringToDate(time, DateTools.DATE_FORMAT_FULL);
        long mills = DateTools.getTimeMills(time, DateTools.DATE_FORMAT_FULL);
        long nowMills = DateTools.getNowTimeMills();
        long result = DateTools.compareTime(nowMills, mills);
        String res = DateTools.getResult(result);
        if("1".equals(res)){
            System.out.println(pro[i].getProductId()+","+pro[i].getProductName()+","+DateTools.formatDateToString(day, DateTools.DATE_FORMAT_FULL));
        }else{
            System.out.println(pro[i].getProductId()+","+pro[i].getProductName()+","+res);
        }

    }
}

}
————运行结果————–
Java基础-Date和SimpleDateFormat

相关标签: Date SimpleDate