DateFormatUtils.java 13.8 KB
Newer Older
yuquan.zhu 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
package cn.timer.api.utils;

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

/**
 * 
* @ClassName: DateFormatUtils 
* @Description: TODO(日期格式化处理工具类) 
* @author TZQ chess222_com 
* @date 2019年3月2日 下午12:29:29 
*
 */
public class DateFormatUtils {
	
	
	/**
	 * 
	* @Title: getWeekNowDay 
	* @Description: TODO(获取当天是当周的星期几) 
	* @param 
	* @return
	* @throws ParseException
	 */
	public static String getWeekNowDay() throws ParseException {
		int day ;
		String dayS = null;
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		day = cal.get(Calendar.DAY_OF_WEEK)-1 != 0?(cal.get(Calendar.DAY_OF_WEEK)-1):7;
		System.out.println("星期:"+day);
		
		switch (day) {
		case 1:
			dayS = "一";
			break;
		case 2:
			dayS = "二";
			break;
		case 3:
			dayS = "三";
			break;
		case 4:
			dayS = "四";
			break;
		case 5:
			dayS = "五";
			break;
		case 6:
			dayS = "六";
			break;
		case 7:
			dayS = "日";
			break;
		default:
			break;
		}
		
		return "星期"+ dayS;
	}
	
	
	public static Date[] getWeekFirstDay(Date date) throws ParseException {
		Date[] dates = new Date[2];
		int day ;
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		 
		day = cal.get(Calendar.DAY_OF_WEEK)-1 != 0?(cal.get(Calendar.DAY_OF_WEEK)-1):7;
		System.out.println("星期:"+day);
		
		Date date1 = new Date(cal.getTimeInMillis());
		System.out.println("今天:"+date1);
		
		cal.add(Calendar.DAY_OF_MONTH,1-day);
		cal.set(Calendar.HOUR_OF_DAY, 0);  
		cal.set(Calendar.MINUTE, 0);  
		cal.set(Calendar.SECOND, 0); 
		Date date2 = new Date(cal.getTimeInMillis());
		System.out.println("本周一:"+date2);
		
		cal.add(Calendar.DAY_OF_MONTH,6);
		cal.set(Calendar.HOUR_OF_DAY, 23);  
		cal.set(Calendar.MINUTE, 59);  
		cal.set(Calendar.SECOND, 59); 
		Date date3 = new Date(cal.getTimeInMillis());
		System.out.println("本周日:"+date3);
		dates[0] = date2;
		dates[1] = date3;
		cal = null;
		
		return dates;
	}
	
	public static Date getToDay() throws ParseException {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.HOUR_OF_DAY, 0);  
		cal.set(Calendar.MINUTE, 0);  
		cal.set(Calendar.SECOND, 0); 
		Date date = new Date(cal.getTimeInMillis());
		return date;
	}
	
	
	public static Date getDateByString(String date) throws ParseException {
tangzhaoqian committed
112
		if (date == null || "".equals(date)) {
yuquan.zhu committed
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 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
			return null;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date2 = sdf.parse(date);
		
		return date2;
	}
	
	public static Date getDate() throws ParseException {
		Date d = new Date();// 获取时间
		return d ;
	}
	
	
	public static String getSystemDateByLong() throws ParseException {
		Date d = new Date();// 获取时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String Long = sdf.format(d);
		
		return Long;
	}
	
	public static Date toYmdDate(String date) throws ParseException {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date2 = sdf.parse(date);
		
		return date2;
	}
	
	/**
	 * 
	* @Title: getSystemDateByYmd 
	* @Description: TODO(获得格式化后的日期(yyy-MM-dd)的系统时间) 
	* @param 
	* @return 格式化后的日期(yyy-MM-dd)的系统时间
	* @throws ParseException
	 */
	public static String getSystemDateByYmd() throws ParseException {
		Date d = new Date();// 获取时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String ymd = sdf.format(d);
		
		return ymd;
	}

	/**
	 * 
	* @Title: DateFormatUtils 
	* @Description: TODO(将DATE转成想要的string) 
	* @param 
	* @param date
	* @param parm
	* @return String
	 */
	public static String tranDate(Date date, String parm) {
		String dateStr = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(parm);
			dateStr = sdf.format(date);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
		return dateStr;
	}

	/**
	 *
	 * @Title:getSystemDateByYYYYMMDD
	 * @Description: 获得格式化后的日期(yyyyMMddHHmmss)的系统时间
	 * @return
	 * @throws ParseException
	 */
	public static String getSystemDateByYYYYMMDD() throws ParseException {
		Date d = new Date();// 获取时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String ymd = sdf.format(d);
		return ymd;
	}

	/**
	 *
	 * @Title:getSystemDateByYmd
	 * @Description: 获得格式化后的日期(yyyMMdd)的系统时间
	 * @return 格式化后的日期(yyyMMdd)的系统时间
	 */
	public static String getSystemDateByyymmdd() throws ParseException {
		Date d = new Date();// 获取时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String ymd = sdf.format(d);
		return ymd;
	}

	/**
	 *
	 * @Title:getSystemDateByYmd
	 * @Description: 获得格式化后的日期(MM)的系统时间
	 * @return 格式化后的日期(MM)的系统时间
	 */
	public static String getSystemMM() throws ParseException {
		Date d = new Date();// 获取时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		String ymd = sdf.format(d);
		return ymd;
	}

	/**
	 *
	 * @Title:getSystemDateByYmd
	 * @Description: 获得格式化后的日期(yyyMMdd)的系统时间
	 * @return 格式化后的日期(yyyMMdd)的系统时间
	 */
	public static String getSystemYY() throws ParseException {
		Date d = new Date();// 获取时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String ymd = sdf.format(d);
		return ymd;
	}

	/**
	 *
	 * @Title:getSystemDateByYYYYMMDD
	 * @Description: 获得格式化后的日期(yyyyMMddHHmmss)的系统时间
	 * @return
	 * @throws ParseException
	 */
	public static String getSystemDateByYYYYMMDDHHMMSSSSS() throws ParseException {
		Date d = new Date();// 获取时间

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String ymd = sdf.format(d);
		return ymd;
	}

	/**
	 *
	 * @Title:getSystemDateByYYYYMMDD
	 * @Description: 获得格式化后的日期(根据传入的字符串格式)的系统时间
	 * @return
	 * @throws ParseException
	 */
	public static String getSystemDateByParm(String parm) {
		String ymd = null;
		try {
			Date d = new Date();// 获取时间
			SimpleDateFormat sdf = new SimpleDateFormat(parm);
			ymd = sdf.format(d);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return ymd;
	}
	
	/**
	 *
	 * @Title:getSystemDateByYYYYMMDD
	 * @Description: 根据传入的时间戳和字符串格式获取格式化后的时间
	 * @return
	 * @throws ParseException
	 */
	public static String getStringByTimestampAndParm(Long timestamp,String parm) {
		String ymd = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(parm);
			ymd = sdf.format(timestamp);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return ymd;
	}

	/**
	 *
	 * @Title:getMonthDayForDate
	 * @Description: 根据字符型日期,获取该日期所在月份共有多少天
	 * @param dateFormat
	 *            格式化后的日期(yyy-MM-dd)
	 * @return 该日期所在月份共多少天
	 */
	public static int getMonthDayForDate(String dateFormat) throws ParseException {
		Calendar cDay = Calendar.getInstance();
		DateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
		Date date = dd.parse(dateFormat);
		cDay.setTime(date);
		int lastDay = cDay.getActualMaximum(Calendar.DAY_OF_MONTH);
		return lastDay;
	}

	/**
	 *
	 * @Title:getCalendarByYearMonth
	 * @Description: 根据日期(周一yyyy-MM-dd)获取本周都有哪些天。
	 * @param dateFormat
	 * @return
	 * @throws ParseException
	 */
	public static String[] getCalendarByYearMonth(String dateFormat) throws ParseException {

		Calendar cDay = Calendar.getInstance();
		DateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
		Date date = dd.parse(dateFormat);
		cDay.setTime(date);

		String[] weeks = new String[7];
		String sDay0 = "" + cDay.get(Calendar.DATE);
		if (sDay0.length() > 1) {
			weeks[0] = sDay0;
		} else {
			weeks[0] = "0" + sDay0;
		}
		for (int i = 1; i < 7; i++) {
			cDay.add(Calendar.DATE, 1);
			String sDay = "" + cDay.get(Calendar.DATE);
			if (sDay.length() > 1) {
				weeks[i] = sDay;
			} else {
				weeks[i] = "0" + sDay;
			}
		}

		return weeks;
	}

	/**
	 *
	 * @Title:getCalendarByweek
	 * @Description: 根据传递过来的日期,返回当前日期所在周的时间段
	 * @param dateFormat
	 *            日期("2014-09-19")
	 * @return
	 * @throws ParseException
	 */
	public static String[] getCalendarByweek(String dateFormat) throws ParseException {
		String[] resultarr = new String[2];

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cl = Calendar.getInstance();

		Date d = sdf.parse(dateFormat);

		cl.setTime(d); // nd为传过来的日期,Date 型,此步可省为当前日期
		int day_of_week = cl.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		cl.add(Calendar.WEEK_OF_MONTH, 0); // idx 参数,0为当前,1为下周 -1为上周以此类推
		cl.add(Calendar.DATE, -day_of_week + 1);
		String week1 = sdf.format(cl.getTime());// 周一
		resultarr[0] = week1;
		// System.out.println(week1);
		cl.add(Calendar.DATE, +6);
		String week2 = sdf.format(cl.getTime());// 周日
		resultarr[1] = week2;
		// System.out.println(week2);

		return resultarr;
	}

	/**
	 *
	 * @Title:getWeekForDate
	 * @Description: 根据日期(yyyy-MM-dd)获得其是该月的第几周
	 * @param dateFormat
	 *            日期("2014-09-19")
	 * @return
	 * @throws ParseException
	 */
	public static Integer getWeekForDate(String dateFormat) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = sdf.parse(dateFormat);
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONTH);
		calendar.setTime(date);
		int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
		return weekOfMonth;
	}

	/**
	 * @Title: convertToDate @Description: TODO(这里用一句话描述这个方法的作用) @param @param
	 * dateString 要转化的字符串 @param @param parms 转化后的格式,yyyy-MM-dd
	 * HH:mm:ss @param @return 设定文件 @return Date 返回类型 @throws
	 */
	public static Date convertToDate(String dateString, String parms) {
		SimpleDateFormat df = new SimpleDateFormat(parms);
		try {
			Date date = df.parse(dateString);
			return date;
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
		return null;
	}

	/**
	 * @Title: countAge @Description: TODO(计算目标日期距离系统当前日期多少天) @param @param
	 * createDate yyyy-MM-dd HH:mm:ss @param @return 设定文件 @return Long 返回类型 @throws
	 */
	public static Long countAge(String createDate) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Long day = 0l;//字母 l
		try {
			String nowDate = DateFormatUtils.getSystemDateByParm("yyyy-MM-dd");
			long to = df.parse(nowDate).getTime();
			long from = df.parse(createDate).getTime();
			day = (to - from) / (1000 * 60 * 60 * 24);
		} catch (ParseException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		return day;
	}

	/**
	 * @Title: getBetweenDay @Description: TODO(判断两个日期之间相差多少天) @param @param
	 * date1 @param @param date2 @param @return 设定文件 @return int 返回类型 @throws
	 */
	public static int getBetweenDay(Date date1, Date date2) {
		Calendar d1 = new GregorianCalendar();
		d1.setTime(date1);
		Calendar d2 = new GregorianCalendar();
		d2.setTime(date2);
		int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
		int y2 = d2.get(Calendar.YEAR);
		if (d1.get(Calendar.YEAR) != y2) {
			// d1 = (Calendar) d1.clone();
			do {
				days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
				d1.add(Calendar.YEAR, 1);
			} while (d1.get(Calendar.YEAR) != y2);
		}
		return days;
	}

	/**
	 * @Title: getCompareDate @Description: TODO(比较两个日期的大小,精确到秒) @param @param
	 * date1 @param @param date2 @param @return 设定文件 @return Boolean 返回类型 @throws
	 */
	public static Boolean getCompareDate(Date date1, Date date2) {
		java.util.Calendar c1 = java.util.Calendar.getInstance();
		java.util.Calendar c2 = java.util.Calendar.getInstance();
		c1.setTime(date1);
		c2.setTime(date2);
		int result = c1.compareTo(c2);
		if (result <= 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * @throws Exception @Title: getSystemDate @Description: TODO(获取yyyy-MM-dd
	 * HH:mm:ss的Date类型) @param @return 设定文件 @return Date 返回类型 @throws
	 */
	public static Date getSystemDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = df.format(new Date());
		System.out.println(date);
		Date dateSyDate = null;
		try {
			dateSyDate = df.parse(date);
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		}
		return dateSyDate;
	}

	/**
	 * @Title: DateDifferentExample @Description:
	 * TODO(计算出两个日期之间相差多少天多少小时多少分多少秒) @param @param d1 @param @param
	 * d2 @param @return 设定文件 @return String 返回类型 @throws
	 */
	public static String DateDifferentExample(Date d1, Date d2) {
		StringBuilder resultString = new StringBuilder();
		try {
			// 毫秒ms
			long diff = d2.getTime() - d1.getTime();

			long diffSeconds = diff / 1000 % 60;
			long diffMinutes = diff / (60 * 1000) % 60;
			long diffHours = diff / (60 * 60 * 1000) % 24;
			long diffDays = diff / (24 * 60 * 60 * 1000);

			resultString.append(diffDays + "天,");
			resultString.append(diffHours + "小时,");
			resultString.append(diffMinutes + " 分钟");
			resultString.append(diffSeconds + "秒.");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultString.toString();
	}

	public static void main(String[] args) throws ParseException {
514

yuquan.zhu committed
515 516 517
	}

}