这个工具类很简单,我们直接看看代码!
每个方法我都做了详细的注释,最近的需求类似QQ的时间显示格式,我放在最后,主要就是通过时间的截取,来余当前时间做判断!
package com.lm.robin.util;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 时间转化类
*
* @author yuwn
*
*/
public class DateUtil {
public static final String DATE_FORMATE_YYYYMMDD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMATE_YYYYMMDD_HHMM = "yyyy-MM-dd HH:mm";
public static final String DATE_FORMATE_YYYYMMDD = "yyyy-MM-dd";
/**
* Date时间转换为yyyy-MM-dd字符串
*
* @param date
* 转换日期
* @return String
*
*/
public static String formatDate(Date date) {
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMATE_YYYYMMDD);
return df.format(date);
}
/**
* Date时间转换为yyyy-MM-dd HH:mm字符串
*
* @param date
* 转换日期
* @return String
*
*/
public static String formatDate_hm(Date date) {
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMATE_YYYYMMDD_HHMM);
return df.format(date);
}
public static String formatDate(String inputDateString) {
Date date = null;
SimpleDateFormat df = new SimpleDateFormat(DATE_FORMATE_YYYYMMDD_HHMMSS);
try {
date = df.parse(inputDateString);
} catch (ParseException e) {
e.printStackTrace();
}
return df.format(date);
}
/**
* 将时间戳转化为具体时间格式
*
* @param timestamp
* : 时间戳
* @return "yyyy-MM-dd"
*/
public static String TimeStampforDate(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将时间戳转化为具体时间格式
*
* @param timestamp
* : 时间戳
* @return "yyyy-MM-dd HH:mm:ss"
*/
public static String TimeStampforDate_(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将时间戳转化为具体时间格式
*
* @param timestamp
* : 时间戳
* @return "MM月dd日"
*/
public static String TimeStampforDate_1(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将时间戳转化为具体时间格式
*
* @param timestamp
* : 时间戳
* @return "HH:mm"
*/
public static String TimeStampforDate_2(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将时间戳转化为具体时间格式
*
* @param timestamp
* : 时间戳
* @return "MM-dd HH:mm"
*/
public static String TimeStampforDate_3(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将时间戳转化为具体时间格式
*/
public static String TimeStampforDate_4(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日 E HH:mm ");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将时间戳转化为具体时间格式
*/
public static String TimeStampforDate_5(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E HH:mm ");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将时间戳转化为具体时间格式
*/
public static String TimeStampforDate_6(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd E ");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将时间戳转化为具体时间格式
*
* @param timestamp
* : 时间戳
* @return "yyyyMMddHHmmss"
*/
public static String TimeStampforDate_7(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String date = sdf.format(new Date(time));
return date;
}
/**
* 将具体时间格式转化为时间戳
*
* @param date
* @return 返回时间戳
* @throws ParseException
*/
public static String DateforTimeStamp(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = sdf.parse(date);
long time = date1.getTime();
String timestamp = String.valueOf(time);
return timestamp;
}
/**
* 将具体时间格式转化为时间戳
*
* @param date
* format 类似于格式yyyy-MM-dd等
* @return 返回时间戳
* @throws ParseException
*/
public static String DateforTimeStamp(String date, String format) {
String timestamp = "";
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date1 = sdf.parse(date);
long time = date1.getTime();
timestamp = String.valueOf(time);
} catch (Exception e) {
}
return timestamp;
}
/**
* 具体的日期转换
*
* @param date
* :要求传入参数的格式 "yyyy-MM-dd"
* @return 返回具体日期 :例如:星期一
*/
public static String getWeekday(String date) {
SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdw = new SimpleDateFormat("E");
Date d = null;
try {
d = sd.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return sdw.format(d);
}
/**
* 上午还是下午
*
* @param time
* 时
* @return
*/
public static String AMOrPM(String time) {
int times = Integer.parseInt(time);
if (times >= 0 && times <= 12) {
return "上午";
} else {
return "下午";
}
}
/**
* 获得小时和分钟
*
* @param timestamp
* @return
*/
public static String TimeStampforDate1(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date = sdf.format(new Date(time));
return date;
}
/**
* 获得小时,返回int型 传入参数类似1410796800000
*/
public static int TimeStampforHour(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("HH");
String date = sdf.format(new Date(time));
return Integer.parseInt(date);
}
/**
* 时间戳转换
*
* @param timestamp
* @return "yyyy年M月d日"
*/
public static String TimeStampforDate3(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日");
String date = sdf.format(new Date(time));
return date;
}
/**
* 时间戳转换
*
* @param timestamp
* @return "HH:mm:ss"
*/
public static String TimeStampforDate4(String timestamp) {
long time = Long.parseLong(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
String date = sdf.format(new Date(time));
return date;
}
/**
* 格式化百分比。如:0.5格式化为50%
*
* @param d
* @return String
* @throws
*/
public static String formatPercentage(double d) {
NumberFormat format = NumberFormat.getPercentInstance();
// format.setMinimumFractionDigits(2);//去除小数位
return format.format(d);
}
/**
* 根据当天日期算出这周的开始时间和结束时间,从周一到周日 timestamp 格式为2014-07-28
*
* @throws ParseException
*/
public static String Datechange(String timestamp) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(timestamp);
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.setTime(date);
calendar2.setTime(date);
int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
int offset1 = 1 - dayOfWeek;
int offset2 = 7 - dayOfWeek;
calendar1.add(Calendar.DATE, offset1);
calendar2.add(Calendar.DATE, offset2);
return formatDate(calendar1.getTime()) + " ~ "
+ formatDate(calendar2.getTime());
}
/**
* 根据当天时间算出这周的周一时间 timestamp 格式为2014-07-28 返回值:1410773636000
*/
public static Long DateMonday(String timestamp) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(timestamp);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 1 - dayOfWeek;
calendar.add(Calendar.DATE, offset);
return calendar.getTimeInMillis();
}
/**
* 根据当天时间算出这周的周日时间 timestamp 格式为2014-07-28 返回值:1410773636000
*/
public static Long DateSunday(String timestamp) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(timestamp);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 7 - dayOfWeek;
calendar.add(Calendar.DATE, offset);
return calendar.getTimeInMillis();
}
/**
* 根据日期算出是这周的周几(周一或周二)
*
* @throws ParseException
*/
public static int DateChangeWeek(String timestamp) throws ParseException {
int week;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(timestamp);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
week = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (week < 0) {
week = 0;
}
return week;
}
/**
* 根据两者间隔的日期时间算出间隔的天数 starttime格式为2014-07-28 07:01
*
* @throws ParseException
*/
public static int DayInterval(String starttime, String endtime)
throws ParseException {
int n = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date startdate = sdf.parse(starttime);
Date enddate = sdf.parse(endtime);
long start = startdate.getTime();
long end = enddate.getTime();
if (end > start) {
long betweenDays = (long) ((end - start) / (1000 * 60 * 60 * 24) + 0.5);
n = (int) betweenDays;
} else {
n = 0;
}
return n;
}
/**
* 根据两者间隔的日期时间算出间隔的时间数 starttime格式为2014-07-28 07:01
*
* @throws ParseException
*/
public static int TimeInterval(String starttime, String endtime)
throws ParseException {
int n = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date startdate = sdf.parse(starttime);
Date enddate = sdf.parse(endtime);
long start = startdate.getTime();
long end = enddate.getTime();
if (end > start) {
long betweenTimes = (long) ((end - start) / (1000 * 60 * 60));
n = (int) betweenTimes;
} else {
n = 0;
}
return n;
}
/**
* 计算年月的最后天数
*
* @param year
* @param month
* @return
*/
public static int getDaysOfMonth(int year, int month) {
int days = 0;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
days = 31;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
}
return days;
}
/**
* 获取当天日期 返回信息 yyyy-MM-dd
*/
public static String getCurrentDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(new Date());
}
/**
* 获取当天日期 返回信息 yyyy
*/
public static String getCurrentYear() {
SimpleDateFormat df = new SimpleDateFormat("yyyy");
return df.format(new Date());
}
/**
* 获取本周第一天 返回信息 yyyy-MM-dd
*/
public static String getWeekFirstDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return df.format(cal.getTime());
}
/**
* 获取本周最后一天 返回信息 yyyy-MM-dd
*/
public static String getWeekEndDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
cal.add(Calendar.WEEK_OF_YEAR, 1);
return df.format(cal.getTime());
}
/**
* 获取本月第一天 返回信息 yyyy-MM-dd
*/
public static String getMonthFirstDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
return df.format(cal.getTime());
}
/**
* 获取本月最后一天 返回信息 yyyy-MM-dd
*/
public static String getMonthEndDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH,
cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return df.format(cal.getTime());
}
/**
* 获取本季度第一天 返回信息 yyyy-MM-dd
*/
public static String getQuarterFirstDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
int currentMonth = cal.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 3) {
cal.set(Calendar.MONTH, 0);
} else if (currentMonth >= 4 && currentMonth <= 6) {
cal.set(Calendar.MONTH, 3);
} else if (currentMonth >= 7 && currentMonth <= 9) {
cal.set(Calendar.MONTH, 4);
} else if (currentMonth >= 10 && currentMonth <= 12) {
cal.set(Calendar.MONTH, 9);
}
cal.set(Calendar.DATE, 1);
return df.format(cal.getTime());
}
/**
* 获取本季度最后一天 返回信息 yyyy-MM-dd
*/
public static String getQuarterEndDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
int currentMonth = cal.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 3) {
cal.set(Calendar.MONTH, 2);
cal.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
cal.set(Calendar.MONTH, 5);
cal.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DATE, 31);
}
return df.format(cal.getTime());
}
/**
*
* time为20150103142324 得到日期 例如"今天","明天","后天","08-17",
* 如果不在今年,需要带上年份,如来"2014-08-04"
*/
public static String getDate(String time) {
String year = time.substring(0, 4);
String month = time.substring(4, 6);
String day = time.substring(6, 8);
String hour = time.substring(8, 10);
String minute = time.substring(10, 12);
String second = time.substring(12, 14);
String strtime = "";
String datestr = year + "-" + month + "-" + day;
String hourstr = hour + ":" + minute + ":" + second;
String curryear = getCurrentYear();
String currday = getCurrentDay();
try {
long time1 = Long.parseLong(DateforTimeStamp(datestr + " "
+ hourstr, DATE_FORMATE_YYYYMMDD_HHMMSS));
// 获取当前时间
long currtime = System.currentTimeMillis();
long difftime = currtime - time1;
if (difftime > 0) {
long min = difftime / (1000 * 60);// 多长分钟
if (min == 0) {
strtime = "刚刚";
} else if (min < 60) {
strtime = min + "分钟前";
} else {
// 判断是否在同一年,不在,显示全部信息,否则,显示月,日信息
if (year.equals(curryear)) {
if (datestr.equals(currday)) {
strtime = "今天" + " " + hour + ":" + minute;
} else {
strtime = month + "-" + day + " " + hour + ":"
+ minute;
}
} else {
strtime = datestr + " " + hour + ":" + minute;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strtime;
}
/**
*
* timestmap为1444468397000 得到日期 例如"今天","明天","后天","08-17",
* 如果不在今年,需要带上年份,如来"2014-08-04"
*/
public static String getDateFromstamp(String timestamp) {
Date date = new Date(Long.parseLong(timestamp));
Calendar cal = Calendar.getInstance();
cal.setTime(date);
String year = String.format("%04d", cal.get(Calendar.YEAR));
String month = String.format("%02d", cal.get(Calendar.MONTH) + 1);
String day = String.format("%02d", cal.get(Calendar.DAY_OF_MONTH));
String hour = String.format("%02d", cal.get(Calendar.HOUR_OF_DAY));
String minute = String.format("%02d", cal.get(Calendar.MINUTE));
String second = String.format("%02d", cal.get(Calendar.SECOND));
String strtime = "";
String datestr = year + "-" + month + "-" + day;
String hourstr = hour + ":" + minute + ":" + second;
String curryear = getCurrentYear();
String currday = getCurrentDay();
try {
long time1 = Long.parseLong(DateforTimeStamp(datestr + " "
+ hourstr, DATE_FORMATE_YYYYMMDD_HHMMSS));
// 获取当前时间
long currtime = System.currentTimeMillis();
long difftime = currtime - time1;
if (difftime > 0) {
long min = difftime / (1000 * 60);// 多长分钟
if (min == 0) {
strtime = "刚刚";
} else if (min < 60) {
strtime = min + "分钟前";
} else {
// 判断是否在同一年,不在,显示全部信息,否则,显示月,日信息
if (year.equals(curryear)) {
if (datestr.equals(currday)) {
strtime = "今天" + " " + hour + ":" + minute;
} else {
strtime = month + "-" + day + " " + hour + ":"
+ minute;
}
} else {
strtime = datestr + " " + hour + ":" + minute;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return strtime;
}
/**
*
* time为20150103142324 得到日期 返回信息为2015-01-03 14:23
*/
public static String getDateTime(String time) {
String year = time.substring(0, 4);
String month = time.substring(4, 6);
String day = time.substring(6, 8);
String hour = time.substring(8, 10);
String minute = time.substring(10, 12);
String curryear = getCurrentYear();
String currday = getCurrentDay();
String strtime = "";
String datestr = year + "-" + month + "-" + day;
String hourstr = hour + ":" + minute;
// 判断是否在同一年,不在,显示全部信息,否则,显示月,日信息
if (year.equals(curryear)) {
if (datestr.equals(currday)) {
strtime = "今天" + " " + hourstr;
} else {
strtime = month + "-" + day + " " + hourstr;
}
} else {
strtime = datestr + " " + hourstr;
}
return strtime;
}
/**
* 获取明天日期 返回信息 yyyy-MM-dd 星期一 18:00
*/
public static String getTomorrowDay() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd E 18:00");
Calendar cal = Calendar.getInstance();
cal.roll(Calendar.DAY_OF_YEAR, 1);
return df.format(cal.getTime());
}
/**
*
* cal为日期 如果不在今年,需要带上年份,如来"2014年08月04日",否则返回"08月04日"
*/
public static String getCalDay(Calendar cal) {
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
String str = "";
if (getCurrentYear().equals(year + "")) {
str = String.format("%02d", month) + "月"
+ String.format("%02d", day) + "日";
} else {
str = year + "年" + String.format("%02d", month) + "月"
+ String.format("%02d", day) + "日";
}
return str;
}
/**
*
* cal为日期 如果不在今年,需要带上年份,如来"2014年08月",否则返回"08月"
*/
public static String getCalMonth(Calendar cal) {
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 1;
int day = cal.get(Calendar.DAY_OF_MONTH);
String str = "";
if (getCurrentYear().equals(year + "")) {
str = String.format("%02d", month) + "月";
} else {
str = year + "年" + String.format("%02d", month) + "月";
}
return str;
}
/**
* 获取上个月的第一天 返回格式yyyy-MM-dd
*/
public static String getFirstDayOfLastMonth() {
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1);
lastDate.add(Calendar.MONTH, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String sLastMonth = sdf.format(lastDate.getTime());
return sLastMonth;
}
/**
* 获取上个月的最后一天 返回格式yyyy-MM-dd
*/
public static String getLastDayOfLastMonth() {
Calendar lastDate = Calendar.getInstance();
lastDate.set(Calendar.DATE, 1);//
lastDate.add(Calendar.MONTH, -1);//
lastDate.roll(Calendar.DATE, -1);//
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String eLastMonth = sdf.format(lastDate.getTime());
return eLastMonth;
}
/**
* 获取昨天日期 返回格式yyyy-MM-dd
*/
public static String getyesterdaydate() {
final Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String yesterday = sdf.format(cal.getTime());
return yesterday;
}
/**
* 需要传入的时间格式 2015——11——27 15:00:42.0 输入的需求大致和QQ的时间一样
* @author yuwn
* @param time
* @return
*/
public static String sameToQq(String time) {
String dateforTimeStamp = null;
try {
dateforTimeStamp = DateUtil.DateforTimeStamp(time);
} catch (ParseException e) {
e.printStackTrace();
}
long TimeMillis = Long.parseLong(dateforTimeStamp);
String year = time.substring(0, 4);
String month = time.substring(5, 7);
String day = time.substring(8, 10);
int intDay = Integer.parseInt(day);
String hour = time.substring(11, 13);
int intHour = Integer.parseInt(hour);
String amOrPM = DateUtil.AMOrPM(hour);
String minute = time.substring(14, 16);
String second = time.substring(18, 19);
String datestr = year + "-" + month + "-" + day;
String hourstr = hour + ":" + minute + ":" + second;
String currentYear = DateUtil.getCurrentYear();
String currentDay = DateUtil.getCurrentDay();
String strcurrentDay = currentDay.substring(8, 10);
int intcurrentDay = Integer.parseInt(strcurrentDay);
long currentTimeMillis = System.currentTimeMillis();
if (currentYear.equals(year)) {
if (currentTimeMillis - TimeMillis <= 3600000) {
long hourmillis = currentTimeMillis - TimeMillis;
long min = hourmillis / 1000 / 60;
return min + "分钟前";
} else if (currentTimeMillis - TimeMillis > 3600000) {
if (day.equals(currentDay)) {
long hourmillis = currentTimeMillis - TimeMillis;
long hour1 = hourmillis / 1000 / 60 / 60;
return hour1 + "小时前";
} else if ((intDay + 1) == intcurrentDay) {
if (amOrPM.equals("下午")) {
int dayHour = intHour - 12;
return "昨天" + String.valueOf(amOrPM) + dayHour + ":"
+ minute;
}
return "昨天" + String.valueOf(amOrPM) + intHour + ":"
+ minute;
} else if ((intDay + 2) == intcurrentDay) {
if (amOrPM.equals("下午")) {
int dayHour = intHour - 12;
return "前天" + String.valueOf(amOrPM) + dayHour + ":"
+ minute;
}
return "前天" + String.valueOf(amOrPM) + intHour + ":"
+ minute;
} else {
if (amOrPM.equals("下午")) {
int dayHour = intHour - 12;
return month + "月" + day + "日" + String.valueOf(amOrPM)
+ dayHour + ":" + minute;
}
return month + "月" + day + "日" + String.valueOf(amOrPM)
+ intHour + ":" + minute;
}
}
} else { // 不再同一年显示完整的时间
return time;
}
return time;
}
}