Java基础-Date和SimpleDateFormat
程序员文章站
2024-03-15 19:24:24
...
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);
}
}
}
}
————运行结果————–
上一篇: InferSent的代码实现
下一篇: MVP模式讲解与实例
推荐阅读
-
Java基础-Date和SimpleDateFormat
-
transient,volatile和strictfp关键字(转) 博客分类: JAVA基础 javatransientvolatilestrictfp
-
Java中Date和Calendar常用方法
-
java8中新的Date和Time详解
-
Java基础十一(private、this关键字和构造函数)
-
java8中新的Date和Time详解
-
Java中Date和Calendar常用方法
-
Java 和 Javascript 的 Date 与 .Net 的 DateTime 之间的相互转换
-
Java基础-2-变量和常量
-
Java多线程和并发基础面试题(问答形式)