DateUtils.java 11.7 KB
Newer Older
jiangjiantao's avatar
jiangjiantao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 78 79 80 81 82 83 84 85 86 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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
package com.miya.fastcashier.utils;



import com.blankj.utilcode.util.StringUtils;

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

/**
 * 日期相关工具类
 */
public class DateUtils {

    public static final String DF_YYYYMMDD = "yyyyMMdd";
    public static final String DF_YYMMDD = "yyMMdd";
    public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String DF_YYYY_MM_DD2 = "yyyy/MM/dd";
    public static final String DF_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String DF_YYYY_MM_DDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String DF_HHMMSS = "HHmmss";
    public static final String DF_HHMMSS2 = "HH:mm:ss";
    public static final String DF_CN_YYYY_MM_DD = "yyyy年MM月dd日";
    public static final String DF_HHMM = "HH:mm";

    /**
     * 带ms的时间戳,与poshub对接时添加
     */
    public static final String DF_YYYY_MM_DD_HHMMSSSS = "yyyy-MM-dd HH:mm:ss.SSS";


    public static final String DF_YYYYMMDDHHMM = "yyyy/MM/dd HH:mm";

    private static final String TAG = "DateUtils";

    /**
     * 将日期转换成yyyy-MM-dd HH:mm:ss.SSS字符串
     *
     * @return yyyy-MM-dd HH:mm:ss.SSS
     */
    public static String format17(Date date) {
        return format(date, DF_YYYY_MM_DD_HHMMSSSS);
    }

    /**
     * 将日期转换成字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String format(Date date, String format) {
        if (date == null) {
            throw new IllegalArgumentException("Param date is null!");
        }
        if (StringUtils.isEmpty(format)) {
            throw new IllegalArgumentException("Param format is blank!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * 将日期转换成yyyyMMddHHmmss字符串
     *
     * @param date
     * @return
     */
    public static String format14(Date date) {
        return format(date, DF_YYYYMMDDHHMMSS);
    }

    /**
     * 将日期转换成yyyy-MM-dd- HH:mm:ss字符串
     *
     * @param date
     * @return
     */
    public static String format18(Date date) {
        return format(date, DF_YYYY_MM_DDHHMMSS);
    }

    public static String format8(Date date) {
        return format(date, DF_YYYYMMDD);
    }

    public static String format6(Date date) {
        return format(date, DF_YYMMDD);
    }

    public static String format10(Date date) {
        return format(date, DF_YYYY_MM_DD);
    }

    public static String formatCN(Date date) {
        return format(date, DF_CN_YYYY_MM_DD);
    }

    public static String formatCustom(Date date, String style) {
        return format(date, style);
    }

    public static Date parse8(String dateStr) throws ParseException {
        return parse(dateStr, DF_YYYYMMDD);
    }

    public static Date parse10(String dateStr) throws ParseException {
        return parse(dateStr, DF_YYYY_MM_DD);
    }

    /**
     * 返回当前日期 yyyyMMdd格式 字符串
     *
     * @return
     */
    public static String getNow8() {
        return format8(new Date());
    }

    /**
     * 返回当前日期 yyyyMMdd格式 字符串
     *
     * @return
     */
    public static String getNow6() {
        return format6(new Date());
    }

    /**
     * 返回当前日期 yyyyMMdd格式 字符串
     *
     * @return
     */
    public static String getNow10() {
        return format10(new Date());
    }

    /**
     * 获取当天0点 date对象
     *
     * @return
     */
    public static Date getToday() {
        Date today = null;
        try {
            today = parse(getNow8(), DF_YYYYMMDD);
        } catch (ParseException e) {
            //unreachable
            today = new Date();
        }
        return today;
    }


    /**
     * 根据时间的字符串转成Date对象
     * yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static Date getDateByDateString(String dateStr) {
        Date today = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DDHHMMSS);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            //unreachable
            today = new Date();
        }
        return today;
    }


    /**
     * 将不同格式的 String 转换成 时间戳
     *
     * @param str
     * @param formatStyle
     * @return
     */
    public static long stringToLong(String str, String formatStyle) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);
        return sdf.parse(str).getTime();//毫秒
    }

    /**
     * 根据时间的字符串转成Date对象
     * yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static Date getDateByDateString2(String dateStr) {
        Date today = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYYMMDDHHMMSS);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            //unreachable
            today = new Date();
        }
        return today;
    }


    public static String getDateStringByTimeStamp(long timestamp) {

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat(DF_YYYY_MM_DDHHMMSS);
        return sdf.format(c.getTime());

    }

    public static String getDateStringByTimeStamp(long timestamp, String pattern) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(timestamp);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(c.getTime());
    }

    /**
     * 获取当年1月1日0点 date对象
     *
     * @return
     */
    public static Date getNowYear() {
        Date year = null;
        try {
            String format = "yyyy";
            year = parse(format(new Date(), format), format);
        } catch (ParseException e) {
            //unreachable
            year = new Date();
        }
        return year;
    }

    /**
     * 获取当月1号0点 date对象
     *
     * @return
     */
    public static Date getNowMonth() {
        Date month = null;
        try {
            String format = "yyyy-MM";
            month = parse(format(new Date(), format), format);
        } catch (ParseException e) {
            //unreachable
            month = new Date();
        }
        return month;
    }

    public static int getMonthSpace(Date date1, Date date2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(date1);
        c2.setTime(date2);
        int result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        return result == 0 ? 1 : Math.abs(result);
    }

    public static int getYearSpace4Now(String date) throws ParseException {

        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        c1.setTime(new Date());
        c2.setTime(sdf.parse(date));
        int result = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
        return result == 0 ? 1 : Math.abs(result);
    }

    public static Date parse(String dateStr, String format) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateStr);
    }

    /**
     * @return 获取当前月第一天:
     */
    public static Date getFirstDateOfCurrentMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        try {
            return parse(format8(c.getTime()), DF_YYYYMMDD);
        } catch (ParseException e) {
            return null;
        }

    }

    /**
     * @return 获取下月第一天:
     */
    public static Date getFirstDateOfNextMonth() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        try {
            return parse(format8(c.getTime()), DF_YYYYMMDD);
        } catch (ParseException e) {
            return null;
        }

    }

    /**
     * @return 获取当前月最后一天
     */
    public static Date getListDateOfCurrentMonth() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        try {
            return parse(format8(c.getTime()), DF_YYYYMMDD);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * @return 当月的总天数
     */
    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 计算时间间隔
     *
     * @param fDate 上次时间
     * @param oDate 本次时间
     * @return 精确到天
     */
    public static int getIntervalDays(Date fDate, Date oDate) {
        if (fDate == null || oDate == null) {
            return -1;
        }
        try {
            fDate = parse(format8(fDate), DF_YYYYMMDD);
            oDate = parse(format8(oDate), DF_YYYYMMDD);
        } catch (ParseException e) {
        }
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        //获得两个时间的毫秒时间差异
        long diff = oDate.getTime() - fDate.getTime();
        Long day = diff / nd;//计算差多少天
        return day.intValue();

    }

    /**
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getDate8(Date date) {
        try {
            return parse8(format8(date));
        } catch (ParseException e) {
            //unreachable
        }
        return null;
    }

    /**
     * 时间加days天
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }

    /**
     * 日期加法运算
     *
     * @param sourceDate
     * @param months     增加的月数,可为负数
     * @return
     */
    public static Date addMonths(Date sourceDate, int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sourceDate);
        calendar.add(Calendar.MONTH, months);
        return calendar.getTime();
    }


    /**
     * 时间戳转换成日期格式字符串
     *
     * @param mseconds 精确到毫秒的字符串
     * @param format
     * @return
     */
    public static String timeStamp2Date(String mseconds, String format) {
        if (mseconds == null || mseconds.isEmpty() || mseconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(mseconds)));
    }


    /**
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return true在时间段内,false不在时间段内
     */
    public static boolean hourMinuteBetween(String startDate, String endDate) {

        try {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            Date now = format.parse(format(new Date(),DF_HHMM));
            Date start = format.parse(startDate);
            Date end = format.parse(endDate);
            long nowTime = now.getTime();
            long startTime = start.getTime();
            long endTime = end.getTime();
            return nowTime >= startTime && nowTime <= endTime;
        } catch (Exception e) {
            return false;
        }

    }

}