package com.hh.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Locale;import java.util.TimeZone;/** * 时间工具类,该类的出现是为了替换DateUtil混乱的方法签名。 在本类的工具方法中,凡涉及到时间的本地化表现形式时,都用String格式。 * 凡涉及到子1970-01-01 08:00:00 至某个时间的值时,都用long来表示 * * 本类提供的方法都是依照中国GTM+08:00的时区来计算。 所有的本地化都是按照简体中文 “zh” 来计算。 * ** 请注意:获取的当前毫秒值因为操作系统的原因,都有误差!而且在间隔很小的时间段内(大约十几毫秒,根据操作系统的不用而不同)获取到的 * 毫秒值可能相同,可以理解为不同操作系统的最小时间单位为十几毫秒。 *
* @version 1.0 * @author lixunhui@qq.com * @since JDK 1.6 **/public class DateTools { // 日期式样,年月日,用横杠离开,例如:2006-12-25,2008-08-08 private static final String YYYY_MM_DD = "yyyy-MM-dd"; // 日期式样时分秒,时分秒用冒号离开, private static final String HH_MI_SS = "HH:mm:ss"; // 日期式样,年月日时分秒,年月日用横杠离开,时分秒用冒号离开, private static final String YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss"; // 中国时区 private static TimeZone zone = TimeZone.getTimeZone("GMT+08:00"); // 中国国家语言格式 private static Locale locale = Locale.CHINESE; private static final SimpleDateFormat FORMAT_YYYY_MM_DD = new SimpleDateFormat(YYYY_MM_DD); private static final SimpleDateFormat FORMAT_HH_MI_SS = new SimpleDateFormat(HH_MI_SS); private static final SimpleDateFormat FORMAT_YYYY_MM_DD_HH_MI_SS = new SimpleDateFormat(YYYY_MM_DD_HH_MI_SS); private static final int YEAR = 24 * 60 * 60 * 365; private static final int MONTH = 24 * 60 * 60 * 30; private static final int DAY = 24 * 60 * 60; private static final int HOUR = 60 * 60; private static final int MINUTE = 60; public static void main(String[] args) { /** * String preMonthFirstDayDate = getPreMonthFirstDayDate(); * System.out.println("preMonthFirstDayDate:" + preMonthFirstDayDate); * long preMonthFirstDayInMillis = getPreMonthFirstDayInMillis(); * System.out.println("preMonthFirstDayInMillis:" + * preMonthFirstDayInMillis); long preMonthFirstDayInSeconds = * getPreMonthFirstDayInSeconds(); * System.out.println("preMonthFirstDayInSeconds:" + * preMonthFirstDayInSeconds); String preMonthFirstDayTime = * getPreMonthFirstDayTime(); System.out.println("preMonthFirstDayTime:" * + preMonthFirstDayTime); String preMonthLastDayDate = * getPreMonthLastDayDate(); System.out.println("preMonthLastDayDate:" + * preMonthLastDayDate); long preMonthLastDayInMillis = * getPreMonthLastDayInMillis(); * System.out.println("preMonthLastDayInMillis:" + * preMonthLastDayInMillis); long preMonthLastDayInSeconds = * getPreMonthLastDayInSeconds(); * System.out.println("preMonthLastDayInSeconds:" + * preMonthLastDayInSeconds); String preMonthLastDayTime = * getPreMonthLastDayTime(); System.out.println("preMonthLastDayTime:" + * preMonthLastDayTime); **/ /** * String currMonthFirstDayDate = getCurrMonthFirstDayDate(); * System.out.println("currMonthFirstDayDate:" + currMonthFirstDayDate); * long currMonthFirstDayInMillis = getCurrMonthFirstDayInMillis(); * System.out.println("currMonthFirstDayInMillis:" + * currMonthFirstDayInMillis); long currMonthFirstDayInSeconds = * getCurrMonthFirstDayInSeconds(); * System.out.println("currMonthFirstDayInSeconds:" + * currMonthFirstDayInSeconds); String currMonthFirstDayTime = * getCurrMonthFirstDayTime(); * System.out.println("currMonthFirstDayTime:" + currMonthFirstDayTime); * long preMonthLastDayInSeconds = getPreMonthLastDayInSeconds(); * System.out.println("preMonthLastDayInSeconds:" + * preMonthLastDayInSeconds); long preMonthLastDayInMillis = * getPreMonthLastDayInMillis(); * System.out.println("preMonthLastDayInMillis:" + * preMonthLastDayInMillis); **/ /** * String currYearFirstDayDate = getCurrYearFirstDayDate(); * System.out.println("currYearFirstDayDate:" + currYearFirstDayDate); * String preYearFirstDayDate = getPreYearFirstDayDate(); * System.out.println("preYearFirstDayDate:" + preYearFirstDayDate); * long preYearFirstDayInMillis = getPreYearFirstDayInMillis(); * System.out.println("preYearFirstDayInMillis:" + * preYearFirstDayInMillis); long preYearFirstDayInSeconds = * getPreYearFirstDayInSeconds(); * System.out.println("preYearFirstDayInSeconds:" * +preYearFirstDayInSeconds); String preYearFirstDayTime = * getPreYearFirstDayTime(); System.out.println("preYearFirstDayTime:" + * preYearFirstDayTime); String preYearLastDayDate = * getPreYearLastDayDate(); System.out.println("preYearLastDayDate:" + * preYearLastDayDate); long preYearLastDayInMillis = * getPreYearLastDayInMillis(); * System.out.println("preYearLastDayInMillis:" + * preYearLastDayInMillis); long preYearLastDayInSeconds = * getPreYearLastDayInSeconds(); * System.out.println("preYearLastDayInSeconds:" + * preYearLastDayInSeconds); String preYearLastDayTime = * getPreYearLastDayTime(); System.out.println("preYearLastDayTime:" + * preYearLastDayTime); **/ /** * for(int i=0;i<5;i++){ long currMonthFirstDayInMillis = * getPreYearFirstDayInMillis(); * System.out.println("currMonthFirstDayInMillis:" + * currMonthFirstDayInMillis); } * * * long parseTime2Second = parseTime2Second("2006-01-01 07:59:59"); long * parseTime2Second2 = parseTime2Second("2006-01-01 08:00:00"); * System.out.println(parseTime2Second); * System.out.println(parseTime2Second2); **/ String parseSecond2Days = parseSecond2Days(99999999); System.out.println(parseSecond2Days); } /** * 获取当前时间的本地化表现形式。 如:2012-07-03 18:15:03 yyyy-MM-dd HH:mm:ss * * @return yyyy-MM-dd HH:mm:ss 本地化表现形式 */ public static String currentTime() { Calendar calendar = Calendar.getInstance(zone, locale); Calendar.getInstance(zone, locale); Date time = calendar.getTime(); String format = FORMAT_YYYY_MM_DD_HH_MI_SS.format(time); return format; } /** * 获取当前日期的本地化表现形式,不包含时间。 如:2012-07-03 yyyy-MM-dd * * @return yyyy-MM-dd 本地化表现形式 */ public static String currentDate() { Calendar calendar = Calendar.getInstance(zone, locale); Date time = calendar.getTime(); String format = FORMAT_YYYY_MM_DD.format(time); return format; } /** * 获取当前时间至1970-01-01 08:00:00 的毫秒值。 如:1341368657874 * * @return long型 毫秒值 */ public static long currentTimeInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); long timeInMillis = calendar.getTimeInMillis(); return timeInMillis; } /** * 获取当前时间至1970-01-01 08:00:00 的秒值。 如:1341368867 该方法由毫秒值除以1000获得 * * @return long型 秒值 */ public static long currentTimeInSecond() { Calendar calendar = Calendar.getInstance(zone, locale); long timeInSecond = calendar.getTimeInMillis() / 1000; return timeInSecond; } /** * @param millis * 距1970-01-01 00:00:00.000的毫秒值 * @return yyyy-MM-dd HH:mm:ss 本地化表现形式 */ public static String parseMillis2Time(long millis) { Calendar calendar = Calendar.getInstance(zone, locale); calendar.setTimeInMillis(millis); Date time = calendar.getTime(); String format = FORMAT_YYYY_MM_DD_HH_MI_SS.format(time); return format; } /** * * @param second * 距1970-1-1 00:00:00.000的秒值 * @return yyyy-MM-dd HH:mm:ss 本地化表现形式 * @see String parseMillis2Time(long millis) * @exception RuntimeException * 如果参数不能按照yyyy-MM-dd HH:mm:ss
* 格式解析,会抛出RuntimeException */ public static String parseSecond2Time(long second) { return parseMillis2Time(second * 1000); } /** * * @param time * "2012-07-04 12:19:34" 必须是yyyy-MM-dd HH:mm:ss
格式 * * @return long型 秒值 * @exception RuntimeException * 如果参数不能按照yyyy-MM-dd HH:mm:ss
* 格式解析,会抛出RuntimeException */ public static long parseTime2Second(String time) { if (time == null || time.trim().length() == 0) { return 0L; } Calendar calendar = Calendar.getInstance(zone, locale); Date date = null; try { date = FORMAT_YYYY_MM_DD_HH_MI_SS.parse(time); } catch (ParseException e) { e.printStackTrace(); throw new RuntimeException("日期时间格式解析错误,请检查格式是否正确", e); } calendar.setTime(date); return calendar.getTimeInMillis() / 1000; } /** * 获取今天在00:00:00
的日期和时间本地化表现形式 * * @returnyyyy-MM-dd HH:mm:ss
本地化表现形式 */ public static String getTodayStartTime() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); String format = FORMAT_YYYY_MM_DD_HH_MI_SS.format(date); return format; } /** * 获取今天在00:00:00
至 1970-01-01 08:00:00 的毫秒值 * * @return long型 毫秒值 */ public static long getTodayStartTimeInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); return date.getTime(); } /** * 获取今天在00:00:00
至 1970-01-01 08:00:00 的秒值 * * @return long型 秒值 * @see long getTodayStartTimeInMillis() */ public static long getTodayStartTimeInSeconds() { return getTodayStartTimeInMillis() / 1000; } public static String getTodayEndTimes() { return parseMillis2Time(getTodayEndTimeInMillis()); } /** * 获取今天在23:59:59 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2012-07-04 23:59:59 至 1970-01-01 08:00:00 的秒值 * * @return long型 秒值 */ public static long getTodayEndTimeInSeconds() { return getTodayEndTimeInMillis() / 1000; } /** * 获取今天在凌晨23:59:59 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2012-07-04 23:59:59 至 1970-01-01 08:00:00 的毫秒值 * * @return long型 毫秒值 */ public static long getTodayEndTimeInMillis() { Calendar todayEnd = Calendar.getInstance(zone, locale); todayEnd.set(Calendar.HOUR, 23); todayEnd.set(Calendar.MINUTE, 59); todayEnd.set(Calendar.SECOND, 59); todayEnd.set(Calendar.MILLISECOND, 999); Date date = todayEnd.getTime(); return date.getTime(); } /** * 获取 自 昨天00:00:00.000 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2012-07-03 00:00:00 至 1970-01-01 08:00:00 的毫秒值 * * @return long型 毫秒值 */ public static long getYesterdayStartTimeInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.DAY_OF_MONTH, -1); Date date = calendar.getTime(); return date.getTime(); } /** * 获取 自 昨天00:00:00 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2012-07-03 00:00:00 至 1970-01-01 08:00:00 的秒值 * * @return long型 秒值 */ public static long getYesterdayStartTimeInSeconds() { return getYesterdayStartTimeInMillis() / 1000; } /** * 获取昨天按照yyyy-MM-dd 00:00:00
本地化表现形式 假如今天是2012-07-04,则返回 * 2012-07-03 00:00:00 * * @return 昨天按照yyyy-MM-dd 00:00:00
本地化表现形式 */ public static String getYesterdayStartTime() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.DAY_OF_MONTH, -1); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD_HH_MI_SS.format(date); } /** * 获取昨天按照yyyy-MM-dd
本地化表现形式 假如今天是2012-07-04,则返回 * 2012-07-03 * * @return 获取昨天按照yyyy-MM-dd
本地化表现形式 */ public static String getYesterdayDate() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.add(Calendar.DAY_OF_MONTH, -1); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD.format(date); } /** * 获取昨天在午夜23:59:59 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2012-07-03 23:59:59 至 1970-01-01 08:00:00 的毫秒值 * * @return long型 毫秒值 */ public static long getYesterdayEndTimeInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.add(Calendar.DAY_OF_MONTH, -1); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); Date date = calendar.getTime(); return date.getTime(); } /** * 获取昨天在午夜23:59:59 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2012-07-03 23:59:59 至 1970-01-01 08:00:00 的秒值 * * @return long型 秒值 */ public static long getYesterdayEndTimeInSeconds() { return getYesterdayEndTimeInMillis(); } /** * 获取上个月第一天的按照yyyy-MM-01 00:00:00
本地化表现形式 * 假如今天是2012-07-04,则返回2012-06-01 00:00:00 * * @return 获取上个月第一天的按照yyyy-MM-01 00:00:00
本地化表现形式 */ public static String getPreMonthFirstDayTime() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD_HH_MI_SS.format(date); } /** * 获取上个月第一天的按照yyyy-MM-01
本地化表现形式 * 假如今天是2012-07-04,则返回2012-06-01 * * @return 获取上个月第一天的按照yyyy-MM-01
本地化表现形式 */ public static String getPreMonthFirstDayDate() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD.format(date); } /** * 获取上个月第一天的00:00:00 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2012-06-30 00:00:00 至 1970-01-01 08:00:00 的毫秒值 * * @return long 毫秒值 */ public static long getPreMonthFirstDayInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); return date.getTime(); } /** * 获取上个月第一天的00:00:00 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2012-06-30 00:00:00 至 1970-01-01 08:00:00 的秒值 * * @return long 秒值 */ public static long getPreMonthFirstDayInSeconds() { return getPreMonthFirstDayInMillis() / 1000; } /** * 获取上个月最后一天的按照yyyy-MM-dd 23:59:59
本地化表现形式 * 假如今天是2012-07-04,则返回2012-06-30 23:59:59 * * @return 获取上个月最后一天的按照yyyy-MM-dd 23:59:59
本地化表现形式 */ public static String getPreMonthLastDayTime() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, -1); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD_HH_MI_SS.format(date); } /** * 获取上个月最后一天的按照yyyy-MM-dd
本地化表现形式 * 假如今天是2012-07-04,则返回2012-06-30 * * @return 获取上个月最后一天的按照yyyy-MM-dd
本地化表现形式 */ public static String getPreMonthLastDayDate() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, -1); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD.format(date); } /** * 获取上个月最后一天的23:59:59 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2012-06-30 23:59:59 至 1970-01-01 08:00:00 的毫秒值 * * @return long 毫秒值 */ public static long getPreMonthLastDayInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.DAY_OF_MONTH, -1); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); Date date = calendar.getTime(); return date.getTime(); } /** * 获取上个月最后一天的23:59:59 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2012-06-30 23:59:59 至 1970-01-01 08:00:00 的秒值 * * @return long 秒值 */ public static long getPreMonthLastDayInSeconds() { return getPreMonthLastDayInMillis() / 1000; } /** * 获取当前月第一天按照yyyy-MM-dd 00:00:00
本地化表现形式 * 假如今天是2012-07-04,则返回2012-07-01 00:00:00 * * @return 获取当前月第一天按照yyyy-MM-dd 00:00:00
本地化表现形式 */ public static String getCurrMonthFirstDayTime() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD_HH_MI_SS.format(date); } /** * 获取当前月第一天按照yyyy-MM-dd
本地化表现形式 * 假如今天是2012-07-04,则返回2012-07-01 * * @return 获取当前月第一天按照yyyy-MM-dd
本地化表现形式 */ public static String getCurrMonthFirstDayDate() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD.format(date); } /** * 获取当前月第一天的00:00:00 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2012-07-01 00:00:00 至 1970-01-01 08:00:00 的毫秒值 * * @return long 毫秒值 */ public static long getCurrMonthFirstDayInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); return date.getTime(); } /** * 获取当前月第一天的00:00:00 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2012-07-01 00:00:00 至 1970-01-01 08:00:00 的秒值 * * @return long 毫秒值 */ public static long getCurrMonthFirstDayInSeconds() { return getCurrMonthFirstDayInMillis() / 1000; } /** * 获取今年第一天按照yyyy-MM-dd 00:00:00
本地化表现形式 * 假如今天是2012-07-04,则返回2012-01-01 * * @return 获取今年第一天按照yyyy-MM-dd
本地化表现形式 */ public static String getCurrYearFirstDayDate() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(calendar.get(Calendar.YEAR), Calendar.JANUARY, 1); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD.format(date); } /** * 获取今年第一天按照yyyy-MM-dd 00:00:00
本地化表现形式 * 假如今天是2012-07-04,则返回2012-01-01 00:00:00 * * @return 获取今年第一天按照yyyy-MM-dd 00:00:00
本地化表现形式 */ public static String getCurrYearFirstDayTime() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(calendar.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD_HH_MI_SS.format(date); } /** * 获取今年第一天的00:00:00 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2012-01-01 00:00:00 至 1970-01-01 08:00:00 的毫秒值 * * @return long 毫秒值 */ public static long getCurrYearFirstDayInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set(calendar.get(Calendar.YEAR), Calendar.JANUARY, 1, 0, 0, 0); Date date = calendar.getTime(); return date.getTime(); } /** * 获取今年第一天的00:00:00 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2012-01-01 00:00:00 至 1970-01-01 08:00:00 的秒值 * * @return long 毫秒值 */ public static long getCurrYearFirstDayInSeconds() { return getCurrYearFirstDayInMillis() / 1000; } /** * 获取去年第一天按照yyyy-MM-dd 00:00:00
本地化表现形式 * 假如今天是2012-07-04,则返回2011-01-01 00:00:00 * * @return 获取今年第一天按照yyyy-MM-dd 00:00:00
本地化表现形式 */ public static String getPreYearFirstDayTime() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set((calendar.get(Calendar.YEAR)) - 1, Calendar.JANUARY, 1, 0, 0, 0); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD_HH_MI_SS.format(date); } /** * 获取去年第一天按照yyyy-MM-dd
本地化表现形式 * 假如今天是2012-07-04,则返回2011-01-01 * * @return 获取今年第一天按照yyyy-MM-dd
本地化表现形式 */ public static String getPreYearFirstDayDate() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set((calendar.get(Calendar.YEAR)) - 1, Calendar.JANUARY, 1); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD.format(date); } /** * 获取去年第一天的00:00:00 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2011-01-01 00:00:00 至 1970-01-01 08:00:00 的毫秒值 * * @return long 毫秒值 */ public static long getPreYearFirstDayInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set((calendar.get(Calendar.YEAR)) - 1, Calendar.JANUARY, 1, 0, 0, 0); calendar.set(Calendar.MILLISECOND, 0); Date date = calendar.getTime(); return date.getTime(); } /** * 获取去年第一天的00:00:00 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2011-01-01 00:00:00 至 1970-01-01 08:00:00 的秒值 * * @return long 秒值 * @see getPreYearFirstDayInMillis() */ public static long getPreYearFirstDayInSeconds() { return getPreYearFirstDayInMillis() / 1000; } /** * 获取去年最后一天按照yyyy-MM-dd 23:59:59
本地化表现形式 * 假如今天是2012-07-04,则返回2011-12-31 23:59:59 * * @return 获取去年最后一天按照yyyy-MM-dd 23:59:59
本地化表现形式 */ public static String getPreYearLastDayTime() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set((calendar.get(Calendar.YEAR)) - 1, Calendar.DECEMBER, 31, 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD_HH_MI_SS.format(date); } /** * 获取去年最后一天按照yyyy-MM-dd
本地化表现形式 * 假如今天是2012-07-04,则返回2011-12-31 * * @return 获取去年最后一天按照yyyy-MM-dd
本地化表现形式 */ public static String getPreYearLastDayDate() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set((calendar.get(Calendar.YEAR)) - 1, Calendar.DECEMBER, 31); calendar.set(Calendar.MILLISECOND, 999); Date date = calendar.getTime(); return FORMAT_YYYY_MM_DD.format(date); } /** * 获取去年最后一天的00:00:00 至 1970-01-01 08:00:00 的毫秒值 假如今天是 2012-07-04,则返回 * 2011-12-31 23:59:59 至 1970-01-01 08:00:00 的毫秒值 * * @return long 毫秒值 */ public static long getPreYearLastDayInMillis() { Calendar calendar = Calendar.getInstance(zone, locale); calendar.set((calendar.get(Calendar.YEAR)) - 1, Calendar.DECEMBER, 31, 23, 59, 59); calendar.set(Calendar.MILLISECOND, 999); Date date = calendar.getTime(); return date.getTime(); } /** * 获取去年最后一天的00:00:00 至 1970-01-01 08:00:00 的秒值 假如今天是 2012-07-04,则返回 * 2011-12-31 23:59:59 至 1970-01-01 08:00:00 的秒值 * * @return long 秒值 * @see getPreYearLastDayInMillis() */ public static long getPreYearLastDayInSeconds() { return getPreYearLastDayInMillis() / 1000; } /** * 将 秒 换算成 X年X月X天 * * @param second * @return */ public static String parseSecond2Days(int second) { int year = second / YEAR; int year_remainder = second % YEAR; int month = year_remainder / MONTH; int month_remainder = year_remainder % MONTH; int day = month_remainder / DAY; if (year == 0) { if (month == 0) { return day + "天"; } else { return month + "月" + day + "天"; } } else { return year + "年" + month + "月" + day + "天"; } }}