Java封装的与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前
2013-10-09 10:09
411 查看
public class CalendarCal {
/**
* 与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前
*
* @param calendar
* 与当前时间比较的日期值
* @return 格式化之后的字符串
*/
public String getDateCompareNow(Calendar calendar) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR) - calendar.get(Calendar.YEAR);
String strdate = null;
if (year > 0) {
strdate = year + "年前";
} else if (year == 0) {
int month = cal.get(Calendar.MONTH) - calendar.get(Calendar.MONTH);
if (month > 0) {
strdate = month + "月前";
} else if (month == 0) {
int day = cal.get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH);
if (day > 0) {
strdate = day + "天前";
} else if (day == 0) {
int hour = cal.get(Calendar.HOUR_OF_DAY) - calendar.get(Calendar.HOUR_OF_DAY);
if (hour > 0) {
strdate = hour + "小时前";
} else if (hour == 0) {
int minute = cal.get(Calendar.HOUR_OF_DAY) - calendar.get(Calendar.HOUR_OF_DAY);
if (minute > 0) {
strdate = minute + "分钟前";
} else if (minute == 0) {
strdate = "刚刚";
}
}
// SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); //
// 格式化对象
// strdate = sdf.format(calendar.getTime()); // 输出格式化的日期
// strdate = DateUtil.dateToString(date,"HH:mm");
}
}
} else {
return "-1"; // 返回了一个负数
}
return strdate;
}
/**
* 测试代码
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2013-10-02 15:53:10");
Calendar calendar = Calendar.getInstance(); // 日历对象
calendar.setTime(date); // 设置当前日期
CalendarCal t = new CalendarCal();
String strdate = t.getDateCompareNow(calendar);
System.out.println(strdate);
}
}
/**
* 与当前时间比较,得到多少年,多少月,多少天前,多少小时前,多小分钟前
*
* @param calendar
* 与当前时间比较的日期值
* @return 格式化之后的字符串
*/
public String getDateCompareNow(Calendar calendar) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR) - calendar.get(Calendar.YEAR);
String strdate = null;
if (year > 0) {
strdate = year + "年前";
} else if (year == 0) {
int month = cal.get(Calendar.MONTH) - calendar.get(Calendar.MONTH);
if (month > 0) {
strdate = month + "月前";
} else if (month == 0) {
int day = cal.get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH);
if (day > 0) {
strdate = day + "天前";
} else if (day == 0) {
int hour = cal.get(Calendar.HOUR_OF_DAY) - calendar.get(Calendar.HOUR_OF_DAY);
if (hour > 0) {
strdate = hour + "小时前";
} else if (hour == 0) {
int minute = cal.get(Calendar.HOUR_OF_DAY) - calendar.get(Calendar.HOUR_OF_DAY);
if (minute > 0) {
strdate = minute + "分钟前";
} else if (minute == 0) {
strdate = "刚刚";
}
}
// SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); //
// 格式化对象
// strdate = sdf.format(calendar.getTime()); // 输出格式化的日期
// strdate = DateUtil.dateToString(date,"HH:mm");
}
}
} else {
return "-1"; // 返回了一个负数
}
return strdate;
}
/**
* 测试代码
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2013-10-02 15:53:10");
Calendar calendar = Calendar.getInstance(); // 日历对象
calendar.setTime(date); // 设置当前日期
CalendarCal t = new CalendarCal();
String strdate = t.getDateCompareNow(calendar);
System.out.println(strdate);
}
}
相关文章推荐
- Java中得到小时、分钟、判断当前时间在不在某个时间之间或之前或之后
- 计算格式为YYYY-MM-DD HH:ii:ss的两个时间,相差多少年、多少月、多少日、多少小时、多少分钟、多少秒?
- Java比较两个时间相差多少天,多少个月,多少年
- 【Java】获取2个日历类型的时间相差多少年、月、天、小时、分钟、秒
- php 显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前
- Java比较两个时间相差多少天,多少个月,多少年
- Java比较两个时间相差多少天,多少个月,多少年
- js和Java中将时间转换为多少天、小时、分钟问题
- delphi 计算文件与当前时间差(天小时分钟秒)
- 【Java】得到当前系统时间,精确到毫秒
- 统计两个时间相隔多少年、月、日、小时、分钟、秒钟(原创)
- [Java] 如何通过当前时间得到日期,月份,年
- ios 获取当前的系统时间 年-月-日 小时-分钟-秒
- Java,.NET中当前时间获取、格式化方法比较
- java设定一个日期时间,加几分钟(小时或者天)后得到新的日期
- java计算两个时间相差(天、小时、分钟、秒)
- js(javascript)取得当前时间小时,分钟,秒 以及毫秒
- java得到当前时间
- java获取当前年、半年、季度、月、日、小时 开始结束时间等
- Java 得到当前系统时间,时间转字符串,字符串转时间,以及求时间差