DateUtil.java 10.4 KB
Newer Older
门心叼龙's avatar
upate  
门心叼龙 已提交
1
package com.fly.tour.common.util;
门心叼龙's avatar
门心叼龙 已提交
2 3 4 5 6 7 8 9 10 11 12

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Description: <DateUtil><br>
M
mxdl 已提交
13
 * Author: mxdl<br>
门心叼龙's avatar
门心叼龙 已提交
14 15 16 17 18 19 20
 * Date: 2018/6/11<br>
 * Version: V1.0.0<br>
 * Update: <br>
 */
public class DateUtil {

    public enum FormatType {
门心叼龙's avatar
门心叼龙 已提交
21
        yyyy, yyyyMM, yyyyMMdd, yyyyMMddHHmm, yyyyMMddHHmmss, MMdd, HHmm,MM,dd,MMddHHmm;
门心叼龙's avatar
门心叼龙 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    }
    /**
     * 格式化时间字符串
     */
    public static String formatDate(String time, String formatStr) {
        Date setdate = parseTime(time);
        SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(setdate);
    }

    public static String formatDate(String time, FormatType type) {
        if (TextUtils.isEmpty(time)) {
            return "";
        }
        Date date = parseTime(time);
        return formatDate(date, type);
    }
门心叼龙's avatar
门心叼龙 已提交
39 40 41 42 43 44 45
    public static String formatDate(String time, FormatType fromtype,FormatType totype) {
        if (TextUtils.isEmpty(time)) {
            return "";
        }
        Date date = parseTime(time,fromtype);
        return formatDate(date, totype);
    }
门心叼龙's avatar
门心叼龙 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    public static String formatDate(Date time, FormatType type) {
        if (time == null) {
            return "";
        }
        SimpleDateFormat sdf = getSimpleDateFormat(type);
        return sdf.format(time);
    }

    private static SimpleDateFormat getSimpleDateFormat(FormatType type) {
        SimpleDateFormat sdf;
        switch (type) {
            case yyyy:
                sdf = new SimpleDateFormat("yyyy");
                break;
            case yyyyMM:
                sdf = new SimpleDateFormat("yyyy-MM");
                break;
            case yyyyMMdd:
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                break;
            case yyyyMMddHHmm:
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                break;
            case yyyyMMddHHmmss:
                sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case MMdd:
                sdf = new SimpleDateFormat("MM-dd");
                break;
            case HHmm:
                sdf = new SimpleDateFormat("HH:mm");
                break;
门心叼龙's avatar
门心叼龙 已提交
78 79 80 81 82 83
            case MM:
                sdf = new SimpleDateFormat("MM");
                break;
            case dd:
                sdf = new SimpleDateFormat("dd");
                break;
门心叼龙's avatar
门心叼龙 已提交
84 85 86
            case MMddHHmm:
                sdf = new SimpleDateFormat("MM-dd HH:mm");
                break;
门心叼龙's avatar
门心叼龙 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
            default:
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                break;
        }
        return sdf;
    }
    /**
     * 将时间字符串转化为date
     */
    public static Date parseTime(String dateStr, String formatStr) {
        Date date = null;
        try {
            DateFormat sdf = new SimpleDateFormat(formatStr);
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 将字符串转换成date
     */
    public static Date parseTime(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }
    /**
     * 将字符串转换成date
     */
    public static Date parseTime(String time, FormatType type) {
        Date date = null;
        SimpleDateFormat sdf = getSimpleDateFormat(type);
        try {
            date = sdf.parse(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 增/减 offset 后的日期
     */
    public static Date addAndSubtractDate(int offset, Date date, int unit) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (unit == Calendar.MONTH) {
            calendar.set(Calendar.DATE, 1);
        }
        calendar.set(unit, calendar.get(unit) + offset);
        return calendar.getTime();
    }

    /**
     * 计算两个日期之间相差的天数
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 比较日期大小
     */
    public static int compareDate(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        if (time1 == time2) {
            return 0;
        } else if (time1 > time2) {
            return -1;
        } else {
            return 1;
        }

    }

    /**
     * 根据日期算周几
     */
    public static String whatDay(Date date) {
        Calendar calendar = Calendar.getInstance();// 获得一个日历
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DATE);
        calendar.set(year, month, day);// 设置当前时间,月份是从0月开始计算
        int number = calendar.get(Calendar.DAY_OF_WEEK);// 周表示1-7,是从周日开始,
        String[] str = {"", "周日", "周一", "周二", "周三", "周四", "周五", "周六",};
        return str[number];
    }

    public static String formatSecondToHourMinuteSecond(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }
        return h + "时" + d + "分" + s + "秒";
    }

    public static String formatSecondToHourMinute(int duration) {
        if (duration < 60) {
            return duration + "秒";
        } else if (duration < 60 * 60) {
            return Math.round((float) duration / 60) + "分钟";
        } else {
            int second = duration % (60 * 60);
            if (second == 0) {
                return (duration / (60 * 60)) + "小时";
            } else {

                int round = Math.round((float) second / 60);
                if (round == 0) {
                    return (duration / (60 * 60)) + "小时";
                } else {
                    if (round == 60) {
                        return ((duration / (60 * 60)) + 1) + "小时";
                    } else {
                        return (duration / (60 * 60)) + "小时" + round + "分钟";
                    }
                }
            }
        }
    }

    /**
     * 说明 小于1分钟:”刚刚“ 小于1小时:”X分钟前“ 小于一天:”X小时前“ 小于一月:”X天前“ 小于一年:6-23 大于一年:2015-6-23
     *
     * @param dateStr
     * @return
     */
    public static String formatTimeToDay(String dateStr) {
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat sdf2 = new SimpleDateFormat("MM-dd");
        DateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = sdf.parse(dateStr);

            int minute = (int) ((System.currentTimeMillis() - date.getTime()) / 1000 / 60);
            if (minute <= 0) {
                return "刚刚";

            } else if (minute / 60 == 0) {
                return minute + "分钟前";

            } else if (minute / (60 * 24) == 0) {
                return minute / 60 + "小时前";

            } else if (minute / (60 * 24 * 30) == 0) {
                return minute / (60 * 24) + "天前";
            } else if (minute / (60 * 24 * 30 * 12) == 0) {
                return sdf2.format(date);
            } else {
                return sdf3.format(date);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return dateStr;
    }

    /**
     * 获取几个小时以后的时间戳
     *
     * @param hour
     * @return
     */
    public static String getLaterTimeByHour(int hour) {
        Date d = new Date();
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.HOUR, now.get(Calendar.HOUR) + hour);
        // now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + 30);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(now.getTime());
    }

    /**
     * 获取几天以后的时间戳
     *
     * @param day
     * @return
     */
    public static String getLaterTimeByDay(int day) {
        return getLaterTimeByHour(day * 24);
    }
门心叼龙's avatar
门心叼龙 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

    /**
     * 获取给定时间以后几天的时间戳
     * @param date
     * @param day
     * @return
     */
    public static String getLaterTimeByDay(String date,int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseTime(date, FormatType.yyyyMMdd));
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + day * 24);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }
    /**
     * 获取当前时间的位置:一天24小时以半小时为单位划分为48个单元格
     * @return
     */
    public static int getCurrTimePosition() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return (int)Math.ceil((double)(calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)) / 30);
    }
门心叼龙's avatar
门心叼龙 已提交
337
}