常用的时间工具类

常用的几个格式的时间转换
public class MyTimeUtil {
    /**
     * 年-月-日
     */
    public static final String DATE2Y = "yy-MM-dd";
    /**
     * 年-月-日 时:分
     */
    public static final String DATE2Y_TIME = "yy-MM-dd HH:mm";

    /**
     * 月-日 时:分
     */
    public static final String DATE_MONTH_TIME = "MM-dd HH:mm";

    /**
     * 时 : 分
     */
    public static final String TIEM_HOUR_MINUTE = "HH:mm";

    /**
     * 年-月-日
     */
    public static final String DATE4Y = "yyyy-MM-dd";
    /**
     * 年-月-日 时:分
     */
    public static final String DATE4Y_TIME = "yyyy-MM-dd HH:mm";
    /**
     * 年-月-日 时:分:秒
     */
    public static final String DATE4Y_TIME_SECOND = "yyyy-MM-dd HH:mm:ss";

    /**
     * 得到 yy-MM-dd HH:mm 格式的日期时间字符串
     * <p/>
     * author km
     * param time
     * return
     * postscript
     */
    public static String getDate2YTime(long time) {
        SimpleDateFormat format = new SimpleDateFormat(DATE2Y_TIME);
        return format.format(new Date(time));
    }

    /**
     * 得到 HH:mm 格式的时间字符串
     * <p/>
     * author km
     * param time
     * return
     * postscript
     */
    public static String getHourAndMin(long time) {
        SimpleDateFormat format = new SimpleDateFormat(TIEM_HOUR_MINUTE, Locale.CHINA);
        return format.format(new Date(time));
    }

    /**
     * 将10位的时间戳格式转换
     *
     * @param dataFormat
     * @param timeStamp
     * @return
     */
    public static String formatData(String dataFormat, long timeStamp) {
        if (timeStamp == 0) {
            return "";
        }
        timeStamp = timeStamp * 1000;
        String result = "";
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        result = format.format(new Date(timeStamp));
        return result;
    }

    /**
     * Get the time to chat
     */
    public static String getChatTime(long timesamp) {
        String result = null;
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        Date today = new Date(System.currentTimeMillis());
        Date otherDay = new Date(timesamp);
        int temp = Integer.parseInt(sdf.format(today)) - Integer.parseInt(sdf.format(otherDay));
        switch (temp) {
            case 0:
                result = "今天 " + getHourAndMin(timesamp);
                break;
            case 1:
                result = "昨天 " + getHourAndMin(timesamp);
                break;
            case 2:
                result = "前天 " + getHourAndMin(timesamp);
                break;
            default:
                // result = temp + "天前 ";
                result = getDate2YTime(timesamp);
                break;
        }
        return result;
    }

    /**
     * 错后显示今天、昨天、前天
     *
     * @param date
     * @return
     */
    public static String getTodayOrYesterday(long date) {//date 是存储的时间戳
        //所在时区时8,系统初始时间是1970-01-01 80:00:00,注意是从八点开始,计算的时候要加回去
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis() + offSet) / 86400000;
        long start = (date * 1000 + offSet) / 86400000;
        long intervalTime = start - today;
        //-2:前天,-1:昨天,0:今天,1:明天,2:后天
        String strDes = "";
        String times = formatData("yyyy-MM-dd HH:mm", date);
        String time = times.substring(11, 16);
        if (intervalTime == 0) {
            strDes = "今天" + time;//今天
        } else if (intervalTime == -1) {
            strDes = "昨天" + time;//昨天  intervalTime
        } else if (intervalTime == -2) {
            strDes = "前天" + time;//前天  intervalTime
        } else {
            strDes = MyTimeUtil.formatData("yyyy-MM-dd HH:mm", date);//直接显示时间
        }
        return strDes;
    }

    /**
     * 获取定制时间的格式
     * <p/>
     * param standardTime
     * return
     */
    public static String getCustomizedTime(String standardTime) {
        Date now = new Date();
        Date chatTime = StringToDate(standardTime);
        long minuties = (now.getTime() - chatTime.getTime()) / 60000; // 当前时间和发帖时间的间隔分钟

        if (minuties < 60) {
            return minuties + "分钟前";
        } else if (minuties < 1440 && minuties >= 60) {
            return minuties / 60 + "小时前";
        } else if (minuties > 1439 && minuties < 1440 * 2) {
            return "昨天";
        } else if (minuties < 1440 * 7 && minuties > 1440 * 2) {
            int day = (int) (minuties / 1440);
            return day + "天前";
        } else if (minuties >= 1440 * 7) {
            return DateToYMDString(chatTime);
        } else {
            return standardTime;
        }
    }

    /**
     * 日期转换成 日期时间格式为 MM-dd HH:mm 的字符串
     * <p/>
     * param date
     * return
     */
    public static String DateToMonthString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_MONTH_TIME);
        return sdf.format(date);
    }

    /**
     * 日期转换成 时间格式为 HH:mm 的字符串
     * <p/>
     * param date
     * return
     */
    public static String DateToHourString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(TIEM_HOUR_MINUTE);
        return sdf.format(date);
    }

    /**
     * 字符串转换成日期 格式为 yyyy-MM-dd HH:mm
     * <p/>
     * param str
     * return
     */
    public static Date StringToDate(String str) {
        SimpleDateFormat format = new SimpleDateFormat(DATE4Y_TIME);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 日期转换成日期格式为 yyyy-MM-dd 的字符串
     * <p/>
     * param date
     * return
     */
    public static String DateToYMDString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE4Y, Locale.CHINA);
        return sdf.format(date);
    }

    public static int getDateWeek(String strDate) {
        // String subTime = strDate.substring(0, 10);
        SimpleDateFormat sdf = new SimpleDateFormat(DATE4Y,
                Locale.CHINA);
        Date date = null;
        try {
            date = sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return -1;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        // calendar.get
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return week;
    }

    /**
     * 截取 时间格式为  2016-03-26T10:00:00 中的日期
     * author km
     * param time
     * return 如果time 不等于null ,返回 日期格式  2016-03-26
     * postscript
     */
    public static String removeTail(String time) {
        if (time == null) {
            return null;
        }
        time = time.substring(0, time.indexOf('T'));
        return time;
    }

    /**
     * 替换掉时间格式为  2016-03-26T10:00:00  中的 T 字符
     * author km
     * param time
     * return 如果time不等于 null,返回格式为 2016-03-26 10:00:00
     * postscript
     */
    public static String removeT(String time) {
        if (time == null) {
            return null;
        }
        time = time.replace('T', ' ');
        return time;
    }

    /**
     * 得到系统当前时间,时间格式为  yyyy-MM-dd HH:mm:ss
     * author km
     * return
     */
    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE4Y_TIME_SECOND, Locale.CHINA);
        String time = sdf.format(new Date());
        return time;
    }

    /**
     * 将时间格式化为  年/月/日格式
     *
     * @param fileinsaledate
     * @return
     */

    public static String getFormartData(String fileinsaledate) {
        StringBuffer stringBuffer = null;
        if (!("".equals(fileinsaledate) && fileinsaledate == null)) {
            stringBuffer = new StringBuffer();
            stringBuffer.append(fileinsaledate.substring(0, 4)).append("/" + fileinsaledate.substring(4, 6)).append("/" + fileinsaledate.substring(6));
        }
        return stringBuffer.toString();
    }

    /**
     * 获取指定格式日期的时间戳
     *
     * @param time
     * @return
     */
    public static String getTimeStamp(String time) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        Date d;
        try {
            d = sdf.parse(time);
//            long l = d.getTime() - 8 * 3600*1000;
            long l = d.getTime();
            String str = String.valueOf(l);
            result = str.substring(0, 13);
        } catch (ParseException e) {
            // TODO Auto-generated catch block e.printStackTrace();
        }
        MyLog.e("time", result);
        return result;
    }

    /**
     * 获取指定格式日期的时间戳
     * yyyy-MM-dd
     *
     * @param time
     * @return
     */
    public static String getNewTimeStamp(String time) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Date d;
        try {
            d = sdf.parse(time);
//            long l = d.getTime() - 8 * 3600*1000;
            long l = d.getTime();
            String str = String.valueOf(l);
            result = str.substring(0, 13);
        } catch (ParseException e) {
        }
        MyLog.e("time", result);
        return result;
    }


    /**
     * 格式为yyyy-M-d转化为时间戳
     *
     * @param time
     * @return
     */
    public static String getTimeStamps(String time) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d", Locale.CHINA);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        Date d;
        try {
            d = sdf.parse(time);
//            long l = d.getTime() - 8 * 3600*1000;
            long l = d.getTime();
            String str = String.valueOf(l);
            result = str.substring(0, 13);
        } catch (ParseException e) {
            // TODO Auto-generated catch block e.printStackTrace();
        }
        MyLog.e("time", result);
        return result;
    }

    /**
     * 根据时间判断两个时间戳是否是同一天
     */
    public static final int SECONDS_IN_DAY = 60 * 60 * 24;
    public static final long MILLIS_IN_DAY = 1000L * SECONDS_IN_DAY;

    public static boolean isSameDayOfMillis(final long ms1, final long ms2) {
        final long interval = ms1 - ms2;
        return interval < MILLIS_IN_DAY
                && interval > -1L * MILLIS_IN_DAY
                && toDay(ms1) == toDay(ms2);
    }

    private static long toDay(long millis) {
        return (millis + TimeZone.getDefault().getOffset(millis)) / MILLIS_IN_DAY;
    }

    /**
     * 获得当前时间是星期几
     *
     * @return
     */
    public static String getWay() {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return mWay;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值