Andorid 日期时间工具类

日期时间工具类
格式化日期时间

 Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);

解析字符串格式的日期时间

  SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date dateformatter.parse("2017-07-10 12:23:00");
        } catch (ParseException e) {
            return null;
        }

封装 TimeUtil

public class TimeUtil {

   /**
   *一些时间格式
   */
    public final static String FORMAT_TIME = "HH:mm";
    public final static String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm";
    public final static String FORMAT_DATE_TIME_SECOND = "yyyy-MM-dd HH:mm:ss";
    public final static String FORMAT_MONTH_DAY_TIME = "MM-dd HH:mm";
    public final static String FORMAT_DATE = "yyyy-MM-dd";

    public static String getFormatToday(String dateFormat) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(currentTime);
    }

    public static Date stringToDate(String dateStr, String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        try {
            return formatter.parse(dateStr);
        } catch (ParseException e) {
            return null;
        }
    }
    public static String dateToString(Date date, String dateFormat) {
        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
        return formatter.format(date);
    }

   /**
   *类似QQ/微信 聊天消息的时间
   */
    public static String getChatTime(boolean hasYear, long timesamp) {
        long clearTime = timesamp;
        String result;
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date today = new Date(System.currentTimeMillis());
        Date otherDay = new Date(clearTime);
        int temp = Integer.parseInt(sdf.format(today))
                - Integer.parseInt(sdf.format(otherDay));
        switch (temp) {
            case 0:
                result = "今天 " + getHourAndMin(clearTime);
                break;
            case 1:
                result = "昨天 " + getHourAndMin(clearTime);
                break;
            case 2:
                result = "前天 " + getHourAndMin(clearTime);
                break;
            default:
                result = getTime(hasYear,clearTime);
                break;
        }
        return result;
    }

    private static String getTime(boolean hasYear, long time) {
        String pattern=FORMAT_DATE_TIME;
        if(!hasYear){
            pattern = FORMAT_MONTH_DAY_TIME;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date(time));
    }

    private static String getHourAndMin(long time) {
        SimpleDateFormat format = new SimpleDateFormat(FORMAT_TIME);
        return format.format(new Date(time));
    }

    /**
     * 获取当前日期是星期几
* * @param date * @return 当前日期是星期几 */
public static String getWeekOfDate(Date date) { String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; Calendar cal = Calendar.getInstance(); cal.setTime(date); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } /** * 判断某个日期与今天相差 x年x天 * 如果大于1年,显示x年x天 。否则显示 x天 * @param timeStamp * @return */ public static String getOffsetYearAndDayByTimeStamp(long timeStamp) { String result; long currentTimeStamp = System.currentTimeMillis(); long targetTimestamp = timeStamp * 1000L; int days = (int) ((currentTimeStamp - targetTimestamp) / ONE_YEAr); if (days > 365) { //大于1年 Calendar calendar1 = Calendar.getInstance(); calendar1.setTimeInMillis(currentTimeStamp); Calendar calendar2 = Calendar.getInstance(); calendar2.setTimeInMillis(targetTimestamp); //先判断是否同年 int y1 = calendar1.get(Calendar.YEAR); int y2 = calendar2.get(Calendar.YEAR); int d1 = calendar1.get(Calendar.DAY_OF_YEAR); int d2 = calendar2.get(Calendar.DAY_OF_YEAR); result = (y1 - y2) + "年" + (d1 - d2); } else { //不满一年,直接返回天数 result = String.valueOf(days); } return result; } /** * 描述:计算两个日期所差的天数. * * @param date1 第一个时间的毫秒表示 * @param date2 第二个时间的毫秒表示 * @return int 所差的天数 */ public static int getOffsetDay(long date1, long date2) { Calendar calendar1 = Calendar.getInstance(); calendar1.setTimeInMillis(date1); Calendar calendar2 = Calendar.getInstance(); calendar2.setTimeInMillis(date2); //先判断是否同年 int y1 = calendar1.get(Calendar.YEAR); int y2 = calendar2.get(Calendar.YEAR); int d1 = calendar1.get(Calendar.DAY_OF_YEAR); int d2 = calendar2.get(Calendar.DAY_OF_YEAR); int maxDays = 0; int day = 0; if (y1 - y2 > 0) { maxDays = calendar2.getActualMaximum(Calendar.DAY_OF_YEAR); day = d1 - d2 + maxDays; } else if (y1 - y2 < 0) { maxDays = calendar1.getActualMaximum(Calendar.DAY_OF_YEAR); day = d1 - d2 - maxDays; } else { day = d1 - d2; } return day; } /** * 描述:计算两个日期所差的小时数. * * @param date1 第一个时间的毫秒表示 * @param date2 第二个时间的毫秒表示 * @return int 所差的小时数 */ public static int getOffsetHour(long date1, long date2) { Calendar calendar1 = Calendar.getInstance(); calendar1.setTimeInMillis(date1); Calendar calendar2 = Calendar.getInstance(); calendar2.setTimeInMillis(date2); int h1 = calendar1.get(Calendar.HOUR_OF_DAY); int h2 = calendar2.get(Calendar.HOUR_OF_DAY); int h = 0; int day = getOffsetDay(date1, date2); h = h1 - h2 + day * 24; return h; } /** * 描述:计算两个日期所差的分钟数. * * @param date1 第一个时间的毫秒表示 * @param date2 第二个时间的毫秒表示 * @return int 所差的分钟数 */ public static int getOffsetMinutes(long date1, long date2) { Calendar calendar1 = Calendar.getInstance(); calendar1.setTimeInMillis(date1); Calendar calendar2 = Calendar.getInstance(); calendar2.setTimeInMillis(date2); int m1 = calendar1.get(Calendar.MINUTE); int m2 = calendar2.get(Calendar.MINUTE); int h = getOffsetHour(date1, date2); int m = 0; m = m1 - m2 + h * 60; return m; } /** * 描述:获取本周的某一天. * * @param format the format * @param calendarField the calendar field * @return String String类型日期时间 */ private static String getDayOfWeek(String format, int calendarField) { String strDate = null; try { Calendar c = new GregorianCalendar(); SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format); int week = c.get(Calendar.DAY_OF_WEEK); if (week == calendarField) { strDate = mSimpleDateFormat.format(c.getTime()); } else { int offectDay = calendarField - week; if (calendarField == Calendar.SUNDAY) { offectDay = 7 - Math.abs(offectDay); } c.add(Calendar.DATE, offectDay); strDate = mSimpleDateFormat.format(c.getTime()); } } catch (Exception e) { e.printStackTrace(); } return strDate; } /** * 获取当前日期是星期几 * * @param date * @return 当前日期是星期几 */ public static String getWeekOfDate(Date date) { String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}; Calendar cal = Calendar.getInstance(); cal.setTime(date); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0) w = 0; return weekDays[w]; } /** * 描述:获取本月第一天. * * @param format the format * @return String String类型日期时间 */ public static String getFirstDayOfMonth(String format) { String strDate = null; try { Calendar c = new GregorianCalendar(); SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format); //当前月的第一天 c.set(GregorianCalendar.DAY_OF_MONTH, 1); strDate = mSimpleDateFormat.format(c.getTime()); } catch (Exception e) { e.printStackTrace(); } return strDate; } /** * 描述:获取本月最后一天. * * @param format the format * @return String String类型日期时间 */ public static String getLastDayOfMonth(String format) { String strDate = null; try { Calendar c = new GregorianCalendar(); SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format); // 当前月的最后一天 c.set(Calendar.DATE, 1); c.roll(Calendar.DATE, -1); strDate = mSimpleDateFormat.format(c.getTime()); } catch (Exception e) { e.printStackTrace(); } return strDate; } }

希望这些总结对大家有所帮助,如果有不正确的地方也可以提出来一起进步。 如果觉的还不错的话,就给个赞哈!

你可能感兴趣的:(工具类,android)