Calendar calendar = Calendar.getInstance();
Calendar.getInstance().get(Calendar.YEAR)
获取当前月:Calendar.getInstance().get(Calendar.MONTH) + 1
注意这里要+1,因为月份是从0开始的。calendar.get(Calendar.DAY_OF_MONTH)
calendar.getTimeInMillis()
// 获取去年的年份 public static int getLastYear() { Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, -1); return c.get(Calendar.YEAR); } // 获取下一年年份 public static int getNextYear() { Calendar c = Calendar.getInstance(); c.add(Calendar.YEAR, 1); return c.get(Calendar.YEAR); }
// 获取上一个月的月份 public static int getLastMonth() { Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -1); return c.get(Calendar.MONTH) + 1; } // 获取下一个月的月份 public static int getNextMonth() { Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, 1); return c.get(Calendar.MONTH) + 1; }
calendar.setTime(new Date())
private static SimpleDateFormat DATE_FORMAT_TILL_SECOND = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
/** * 日期字符串转换为Date * @param dateStr * @param format * @return */ public static Date strToDate(String dateStr, String format) { Date date = null; if (!TextUtils.isEmpty(dateStr)) { DateFormat df = new SimpleDateFormat(format); try { date = df.parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } } return date; }
/** * 日期转换为字符串 * @param timeStr * @param format * @return */ public static String dateToString(String timeStr, String format) { // 判断是否是今年 Date date = DateHelper.strToDate(timeStr, format); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 如果是今年的话,才去“xx月xx日”日期格式 if (calendar.get(Calendar.YEAR) == getYear()) { return DATE_FORMAT_TILL_DAY_CURRENT_YEAR.format(date); } return DATE_FORMAT_TILL_DAY_CH.format(date); }
/** * 日期逻辑 * @param dateStr 日期字符串 * @return */ public static String timeLogic(String dateStr) { Calendar calendar = Calendar.getInstance(); calendar.get(Calendar.DAY_OF_MONTH); long now = calendar.getTimeInMillis(); Date date = strToDate(dateStr, DATE_FORMAT); calendar.setTime(date); long past = calendar.getTimeInMillis(); // 相差的秒数 long time = (now - past) / 1000; StringBuffer sb = new StringBuffer(); if (time > 0 && time < 60) { // 1小时内 return sb.append(time + "秒前").toString(); } else if (time > 60 && time < 3600) { return sb.append(time / 60+"分钟前").toString(); } else if (time >= 3600 && time < 3600 * 24) { return sb.append(time / 3600 +"小时前").toString(); }else if (time >= 3600 * 24 && time < 3600 * 48) { return sb.append("昨天").toString(); }else if (time >= 3600 * 48 && time < 3600 * 72) { return sb.append("前天").toString(); }else if (time >= 3600 * 72) { return dateToString(dateStr, DATE_FORMAT); } return dateToString(dateStr, DATE_FORMAT); }
原文地址:http://blog.csdn.net/wwj_748/article/details/42528689