最近的项目中有这样一个需求,根据用户上次的登录时间判断上次登录距今有多久。
package com.pf.bindDate.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeUtils {
private final static long YEAR = 1000 * 60 * 60 * 24 * 365L;
private final static long MONTH = 1000 * 60 * 60 * 24 * 30L;
private final static long DAY = 1000 * 60 * 60 * 24L;
private final static long HOUR = 1000 * 60 * 60L;
private final static long MINUTE = 1000 * 60L;
/**
* 日期转换成字符串
* @param date
* @return str
*/
public static String dateToStr(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);
return str;
}
/**
* 字符串转换成日期
* @param str
* @return date
*/
public static Date strToDate(String str) {
SimpleDateFormat format