DateUtil.java 12.1 KB
Newer Older
1 2 3 4 5
package cn.timer.api.utils;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
6
import java.util.*;
7 8

import cn.hutool.core.util.StrUtil;
9
import org.springframework.util.StringUtils;
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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

public class DateUtil {

	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	/**
	 * 获取0点
	 * 
	 * @return
	 */
	public static Date getStartTime(int day) {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, day);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	/**
	 * 根据时间戳获取0点
	 * 
	 * @return
	 */
	public static Date getStartTime(int day, long time) {
		Calendar todayStart = Calendar.getInstance();
		todayStart.setTimeInMillis(time);
		todayStart.set(Calendar.HOUR_OF_DAY, day);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	/**
	 * 获取23点
	 * 
	 * @return
	 */
	public static Date getnowEndTime(int day) {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, day);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * 根据时间戳获取23点
	 * 
	 * @return
	 */
	public static Date getnowEndTime(int day, long time) {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.setTimeInMillis(time);
		todayEnd.set(Calendar.HOUR_OF_DAY, day);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}
	
	/**
	 * 将String时间转换为时间戳
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static long getFormat(String time) throws ParseException {
		
		if (StrUtil.isNotBlank(time)) {

			switch (time.length()) {
			case 10:
				return getStringTime(time, "yyyy-MM-dd");
			case 16:
				return getStringTime(time, "yyyy-MM-dd HH:mm");
			case 19:
				return getStringTime(time, "yyyy-MM-dd HH:mm:ss");
			default:
				break;
			}
			
			return 0;
			
		}

		return 0;
	}
	
	/**
	 * 将String时间转换为时间戳
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static Date getFormatDate(String time) throws ParseException {
		
		if (StrUtil.isNotBlank(time)) {
			
			switch (time.length()) {
			case 10:
				return getStringDate(time, "yyyy-MM-dd");
			case 16:
				return getStringDate(time, "yyyy-MM-dd HH:mm");
			case 19:
				return getStringDate(time, "yyyy-MM-dd HH:mm:ss");
			default:
				break;
			}
			
			return null;
			
		}
		
		return null;
	}

	/**
	 * 将String时间转换为时间戳
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static Date getStringDate(String time, String format)
			throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.parse(time);
	}
	
	/**
	 * 将String时间转换为 Date
	 * 
	 * @param time
	 * @return
	 * @throws ParseException
	 */
	public static long getStringTime(String time, String format)
			throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = simpleDateFormat.parse(time);
		return date.getTime();
	}

	/**
	 * 根据时间戳获取当前年月日
	 * 
	 * @param time
	 * @return
	 */
	public static String getStringFormat(long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(time);
		return simpleDateFormat1.format(date);
	}
	
	/**
	 * 根据时间戳获取当前年月日
	 * 
	 * @param time
	 * @return
	 */
	public static String getStringFormat(long time,String format) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(format);
		Date date = new Date(time);
		return simpleDateFormat1.format(date);
	}

	/**
	 * 根据时间戳获取当前年月日
	 * 
	 * @param time
	 * @return
	 */
	public static String getStringFormatVehicle(long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date date = new Date(time);
		return simpleDateFormat1.format(date);
	}

	/**
	 * 根据时间戳获取当前时分
	 * 
	 * @param time
	 * @return
	 */
	public static String getStringDate(long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm");
		Date date = new Date(time);
		return simpleDateFormat1.format(date);
	}

	/**
	 * 时间累计增长
	 * 
	 * @param date
	 * @param minute
	 * @return
	 */
	public static String getGrowth(Date date, int minute) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) + minute);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(now.getTime());
	}

	/**
	 * 时间累计增长
	 * 
	 * @param date
	 * @param minute
	 * @return
	 */
	public static String getReduce(Date date, int minute) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.MINUTE, now.get(Calendar.MINUTE) - minute);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(now.getTime());
	}

	/**
	 * 获取当前年份
	 * 
	 * @return
	 */
	public static int getYear() {
		Calendar cal = Calendar.getInstance();
		int year = cal.get(Calendar.YEAR);
		return year;
	}

	/**
	 * 获取当前月份
	 * 
	 * @return
	 */
	public static int getMonth() {
		Calendar cal = Calendar.getInstance();
		int month = cal.get(Calendar.MONTH) + 1;
		return month;
	}

	/**
	 * 获取当前日期
	 * 
	 * @return
	 */
	public static int getDate() {
		Calendar cal = Calendar.getInstance();
		int date = cal.get(Calendar.DATE);
		return date;
	}

	/**
	 * 根据时间戳返回日期时间
	 * 
	 * @param time
	 * @return
	 */
	public static int getDay(Long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd");
		Date date = new Date(time);
		String day = simpleDateFormat1.format(date);
		return Integer.valueOf(day);
	}
	/**
	 * 根据时间戳返回日期时间
	 * 
	 * @param time
	 * @return
	 */
	public static int getMonth(Long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("MM");
		Date date = new Date(time);
		String day = simpleDateFormat1.format(date);
		return Integer.valueOf(day);
	}
	/**
	 * 根据时间戳返回日期时间
	 * 
	 * @param time
	 * @return
	 */
	public static int getYear(Long time) {
		SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy");
		Date date = new Date(time);
		String day = simpleDateFormat1.format(date);
		return Integer.valueOf(day);
	}

	/**
	 * 获取前一个月最后一天
	 * 
	 * @return
	 */
	public static int getLastDay() {
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		String lastDay = sdf.format(calendar2.getTime());
		return Integer.valueOf(lastDay);
	}

	/**
	 * 获取前一个月月份
	 * 
	 * @return
	 */
	public static int getLastMonth() {
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		String lastDay = sdf.format(calendar2.getTime());
		return Integer.valueOf(lastDay);
	}

	/**
	 * 获取前一个月年份
	 * 
	 * @return
	 */
	public static int getLastYear() {
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String lastDay = sdf.format(calendar2.getTime());
		return Integer.valueOf(lastDay);
	}

	/**
	 * 将字符串转化为日期
	 * 
	 * @param timestr
	 * @return
	 */
	public static Date paraseStringToDate(String timestr) {
		Date date = null;
		Format f = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = (Date) f.parseObject(timestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 根据传入的日期获取所在月份所有日期
	 * 
	 * @param d
	 * @return
	 */
	public static List<String> getAllDaysMonthByDate(Date d) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<String> lst = new ArrayList<String>();
		Date date = getMonthStart(d);
		Date monthEnd = getMonthEnd(d);
		while (!date.after(monthEnd)) {
			lst.add(sdf.format(date));
			date = getNext(date);
		}
		return lst;
	}

	private static Date getNext(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);
		return calendar.getTime();
	}

	private static Date getMonthEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		int index = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.add(Calendar.DATE, (-index));
		return calendar.getTime();
	}

	private static Date getMonthStart(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int index = calendar.get(Calendar.DAY_OF_MONTH);
		calendar.add(Calendar.DATE, (1 - index));
		return calendar.getTime();
	}

	/**
	 * 根据年月日获取当月第一天日期时间戳
	 * 
	 * @param time
	 * @return
	 */
	public static long getFirstDayOfMonth(String time) {
		Date date = paraseStringToDate(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		return calendar.getTime().getTime();
	}

	/**
	 * 根据年月日获取当月最后一天日期时间戳
	 * 
	 * @param time
	 * @return
	 */
	public static long getLastDayOfMonth(String time) {
		Date date = paraseStringToDate(time);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		return calendar.getTime().getTime();
	}

	/**
	 * 获取月份起始日期
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMinMonthDate(String date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 获取月份最后日期
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMaxMonthDate(String date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateFormat.parse(date));
		calendar.set(Calendar.DAY_OF_MONTH,
				calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return dateFormat.format(calendar.getTime());
	}
	
	/**
     * 
     * 描述:获取下一个月的第一天.
     * 
     * @return
     */
	public static String getPerFirstDayOfMonth() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

	/**
	 * 获取当前月的所有天数
	 * @param yearMonth
	 * @return
	 */
	public static List<String> getDayByMonth(String yearMonth){
		List<String> list = new ArrayList<>();
		if(StringUtils.isEmpty(yearMonth)){
			return list;
		}
		String[] year_monnth = yearMonth.split("-");
		Calendar calendar = Calendar.getInstance(Locale.CHINA);
		calendar.set(Integer.parseInt(year_monnth[0]), Integer.parseInt(year_monnth[1])-1, 1);
		int year = calendar.get(Calendar.YEAR);//年份
		int month = calendar.get(Calendar.MONTH) + 1;//月份
		int day = calendar.getActualMaximum(Calendar.DATE);
		for (int i = 1; i <= day; i++) {
			String date=null;
			if(month<10 && i<10){
				date = String.valueOf(year)+"-0"+month+"-0"+i;
			}
			if(month<10 && i>=10){
				date = String.valueOf(year)+"-0"+month+"-"+i;
			}
			if(month>=10 && i<10){
				date = String.valueOf(year)+"-"+month+"-0"+i;
			}
			if(month>=10 && i>=10){
				date = String.valueOf(year)+"-"+month+"-"+i;
			}

			list.add(date);
		}
		return list;
	}
516
}