ClockInTool.java 23.1 KB
Newer Older
ilal committed
1 2 3
package cn.timer.api.controller.kqgl;

import java.text.DateFormat;
lal committed
4
import java.text.Format;
ilal committed
5
import java.text.ParseException;
lal committed
6
import java.text.ParsePosition;
ilal committed
7
import java.text.SimpleDateFormat;
leialin committed
8 9
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
ilal committed
10 11
import java.util.ArrayList;
import java.util.Arrays;
ilal committed
12 13
import java.util.Calendar;
import java.util.Date;
lal committed
14
import java.util.GregorianCalendar;
ilal committed
15
import java.util.HashSet;
lal committed
16
import java.util.Iterator;
ilal committed
17
import java.util.LinkedList;
ilal committed
18
import java.util.List;
ilal committed
19
import java.util.Locale;
ilal committed
20
import java.util.Set;
ilal committed
21

ilal committed
22 23
import org.apache.commons.lang3.ArrayUtils;

ilal committed
24
/**
lal committed
25
 * @author lal 2020-05-11
ilal committed
26 27 28
 *
 */
public class ClockInTool {
lal committed
29

ilal committed
30
	static SimpleDateFormat famt = new SimpleDateFormat("yyyy-MM-dd");
lal committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
	public static void main(String[] args) {
		System.out.println(isLegalDate("2020-9-12"));
		System.out.println(isLegalDate("2020-09-12"));
	}
	
	public static boolean isLegalDate(String sDate) {
	    int legalLen = 10;
	    if ((sDate == null) || (sDate.length() != legalLen)) {
	        return false;
	    }
	 
	    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	    try {
	        Date date = formatter.parse(sDate);
	        return sDate.equals(formatter.format(date));
	    } catch (Exception e) {
	        return false;
	    }

	}
lal committed
51
	
ilal committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	public static String[] shuzuqucong(String[] temp) {
	      
        List<String> list = new LinkedList<String>();  
        for(int i = 0; i < temp.length; i++) {  
            if(!list.contains(temp[i])) {  
                list.add(temp[i]);  
            }  
        }  
  
        String[] rowsTemp = list.toArray(new String[list.size()]);  
        if(rowsTemp!=null && rowsTemp.length > 0) {  
            for(int i=0; i<rowsTemp.length; i++) {  
                System.out.println(rowsTemp[i]);  
            }  
        }
		
		return rowsTemp;
	}
ilal committed
70
	
ilal committed
71 72 73 74 75
	public static boolean useArrayUtils(String[] arr, String targetValue) {
	    return ArrayUtils.contains(arr,targetValue);
	}
	
	// 获得上个月月份年份
ilal committed
76
	public static String getLastYearMonth() {
leialin committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90
//		Calendar now = Calendar.getInstance();
//		int month = now.get(Calendar.MONTH);
//		String m;
//		if (month < 10) {
//			m = "0" + month;
//		} else {
//			m = month + "";
//		}
//		return now.get(Calendar.YEAR) + "-" + m;
		
		LocalDate today = LocalDate.now();
		today = today.minusMonths(1);
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");
		return formatters.format(today);
ilal committed
91 92
	}
	
lal committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	/**
     * 将传入的日期的年月得到一个值
     * @param date 日期
     * @return 获取一个唯一的年月数值
     */
    public static int getYearMonth(Date date){
        //传入日期
        Calendar calder = Calendar.getInstance();
        calder.setTime(date);//设置时间
        int year = calder.get(Calendar.YEAR);//获取年份
        int month=calder.get(Calendar.MONTH);//获取月份
        //返回年份乘以100加上月份的值,因为月份最多2位数,
        // 所以年份乘以100可以获取一个唯一的年月数值
        return year*100+month;
    }
	
lal committed
109 110 111 112
	public static double round(double value){
	    return Math.round(value*100)/100.0;
	}
	
lal 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
	 /**
	  *  判断某个字符串是否存在于数组中
	  *  @param stringArray 原数组
	  *  @param source 查找的字符串
	  *  @return 是否找到
	  */
	 public static boolean contains(String[] stringArray, String source) {
	  // 转换为list
	  List<String> tempList = Arrays.asList(stringArray);
	  
	  // 利用list的包含方法,进行判断
	  if(tempList.contains(source))
	  {
	   return true;
	  } else {
	   return false;
	  }
	 }
	
	
	/**
	 * @param ss
	 * @return 去除重复
	 */
	public static String[] duplicate_removal(String[] ss) {
	    List<String> list =new ArrayList<String>();
			for(String s:ss){
				if(!list.contains(s))			//或者list.indexOf(s)!=-1
					list.add(s);
			}
			return list.toArray(new String[list.size()]);
	}
	
lal committed
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
	/**
	 * 将String时间转换为时间戳
	 * 
	 * @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();
	}
	
	
	/**
	 * 根据时间戳获取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();
	}
	
	/**
	 * 根据时间戳获取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();
	}
	
	// 根据传入的日期获取所在月份所有日期
	public static List<String> getAllDaysMonthByDate(Date d){
		List<String> lst = new ArrayList<String>();
		Date date = getMonthStart(d);
		Date monthEnd = getMonthEnd(d);
		while (!date.after(monthEnd)) {
			lst.add(famt.format(date));
			date = getNext(date);
		}
		return lst;
	}
	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();
	}

	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 getNext(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, 1);
		return calendar.getTime();
	}
	
	// 将字符串转化为日期
	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;
	}
236 237 238 239 240 241 242 243 244 245 246
	// 将字符串转化为日期
	public static Date paraseStringToDate(String timestr,String format){
		Date date = null;
		Format f = new SimpleDateFormat(format);
		try {
			date = (Date) f.parseObject(timestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
lal committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
	/***
     * 去除String数组中的空值
     */
    public static String[] deleteArrayNull(String string[]) {
        String strArr[] = string;

        // step1: 定义一个list列表,并循环赋值
        ArrayList<String> strList = new ArrayList<String>();
        for (int i = 0; i < strArr.length; i++) {
            strList.add(strArr[i]);
        }

        // step2: 删除list列表中所有的空值
        while (strList.remove(null));
        while (strList.remove(""));

        // step3: 把list列表转换给一个新定义的中间数组,并赋值给它
        String strArrLast[] = strList.toArray(new String[strList.size()]);

        return strArrLast;
    }
lal committed
268
    
ilal committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    /***
     * 去除String数组中的空值
     */
    public static String[] deleteArrayNull2(String string[]) {
        String strArr[] = string;

        // step1: 定义一个list列表,并循环赋值
        ArrayList<String> strList = new ArrayList<String>();
        for (int i = 0; i < strArr.length; i++) {
            strList.add(strArr[i]);
        }

        // step2: 删除list列表中所有的空值
        while (strList.remove(null));
//        while (strList.remove(""));

        // step3: 把list列表转换给一个新定义的中间数组,并赋值给它
        String strArrLast[] = strList.toArray(new String[strList.size()]);

        return strArrLast;
    }
    
lal committed
291 292 293 294 295 296
    public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
lal committed
297
	
lal committed
298
	/**
lal committed
299 300 301 302 303
	 * @param timeStr 修改的时间
	 * @param num 修改数字
	 * @param numtime
	 * @return 1:增加一年、2:增加一天、3:减10天、4:增加一个月
	 */
lal committed
304 305 306 307 308 309 310 311
	public static String requires_extra_times(String timeStr, int num,int numtime,int daft) {
		
		DateFormat df;
		if(daft == 1) {
			df = new SimpleDateFormat("yyyy-MM-dd");
		}else {
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
lal committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
		
		Date date = null;
		try {
			date = df.parse(timeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);// 设置起时间
		if(numtime == 1) {
			cal.add(Calendar.YEAR, num);// 增加一年
		}else if(numtime == 2) {
			cal.add(Calendar.DATE, num);//增加一天
		}else if(numtime == 3) {
			cal.add(Calendar.DATE, num);//减10天
lal committed
328
		}else if(numtime == 4){
lal committed
329
			cal.add(Calendar.MONTH, num);//增加一个月
lal committed
330 331
		}else if(numtime == 5) {
			cal.add(Calendar.HOUR, num);
lal committed
332 333 334 335 336
		}
		return df.format(cal.getTime());
	}
	
	/**
lal committed
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	 * @param timeStr
	 * @param addnumber
	 * @return 通过java Calendar 实现时间+ 分钟
	 */
	public static String addtime(String timeStr, String addnumber) {
		String str = null;
		try {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = df.parse(timeStr);
			// 时间累计
			Calendar gc = new GregorianCalendar();
			gc.setTime(date);
			gc.add(GregorianCalendar.MINUTE, Integer.parseInt(addnumber));
			str = df.format(gc.getTime());
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return str;
	}
lal committed
358

lal committed
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
	/**
	 * 获取指定年月的第一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getFirstDayOfMonth1(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最小天数
		int firstDay = cal.getMinimum(Calendar.DATE);
		// 设置日历中月份的最小天数
		cal.set(Calendar.DAY_OF_MONTH, firstDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

	/**
	 * 获取指定年月的最后一天
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public static String getLastDayOfMonth1(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DATE);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(cal.getTime());
	}

lal committed
403 404 405 406 407 408 409 410 411 412 413 414
	// 数组容量的扩容,当空间(.length)不够的时候,增加一
	public static String[] arrycopy(String[] ss) {
		// TODO Auto-generated method stub
		String[] ii = new String[ss.length + 1];
		System.arraycopy(ss, 0, ii, 0, ss.length);
		return ii;
	}

	public static String[] replaceNull(String[] str) {
		// 用StringBuffer来存放数组中的非空元素,用“;”分隔
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < str.length; i++) {
lal committed
415
			if ("null".equals(str[i])) {
lal committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
				continue;
			}
			sb.append(str[i]);
			if (i != str.length - 1) {
				sb.append(";");
			}
		}
		// 用String的split方法分割,得到数组
		str = sb.toString().split(";");
		return str;
	}

	/**
	 * 删除数组中的指定值 或者数组中的元素包含指定值
	 * 
	 * @param filters 数组
	 * @param target  指定值
	 * @return
	 */
	public static String[] doChinFilters(String[] filters, String target) {
ilal committed
436
		
ilal committed
437
		
ilal committed
438 439
		target = ClockInTool.Str_Time_formatting(target,"yyyy-MM-dd");
		
lal committed
440 441 442 443 444 445 446 447
		String[] res = null;
		if (filters.length > 0) {
			List<String> tempList = Arrays.asList(filters);
			// Arrays.asList(filters) 迭代器实现类 不支持remove() 删除,多一步转化
			List<String> arrList = new ArrayList<String>(tempList);
			Iterator<String> it = arrList.iterator();
			while (it.hasNext()) {
				String x = it.next();
ilal committed
448 449 450 451
				if(x != null) {
					if (x.indexOf(target) != -1) {
						it.remove();
					}
lal committed
452 453 454 455 456 457 458 459
				}
			}
			res = new String[arrList.size()];
			arrList.toArray(res);
		}
		return res;
	}

lal committed
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 514 515 516 517 518 519 520 521 522 523 524 525
	/**
	 * 获取两个日期之间的所有日期
	 */
	public static List<String> getDays(String startTime, String endTime) {
		// 返回的日期集合
		List<String> days = new ArrayList<String>();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date start = dateFormat.parse(startTime);
			Date end = dateFormat.parse(endTime);

			Calendar tempStart = Calendar.getInstance();
			tempStart.setTime(start);

			Calendar tempEnd = Calendar.getInstance();
			tempEnd.setTime(end);
			tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
			while (tempStart.before(tempEnd)) {
				days.add(dateFormat.format(tempStart.getTime()));
				tempStart.add(Calendar.DAY_OF_YEAR, 1);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return days;
	}

	/**
	 * 拼接字符串
	 */
	public static String listToString(List<String> list) {

		if (list == null) {
			return null;
		}

		StringBuilder result = new StringBuilder();
		boolean first = true;

		// 第一个前面不拼接","
		for (String string : list) {
			if (first) {
				first = false;
			} else {
				result.append(",");
			}
			result.append(string);
		}
		return result.toString();
	}

	/**
	 * 使用java正则表达式去掉多余的.与0
	 * 
	 * @param s
	 * @return string
	 */
	public static String replace(String s) {
		if (null != s && s.indexOf(".") > 0) {
			s = s.replaceAll("0+?$", "");// 去掉多余的0
			s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
		}
		return s;
	}

ilal committed
526 527
	/**
	 * 根据 年、月 获取对应的月份 的 天数
lal committed
528 529 530 531 532 533 534 535 536 537 538
	 */
	public static int getDaysByYearMonth(int year, int month) {
		Calendar a = Calendar.getInstance();
		a.set(Calendar.YEAR, year);
		a.set(Calendar.MONTH, month - 1);
		a.set(Calendar.DATE, 1);
		a.roll(Calendar.DATE, -1);
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}

ilal committed
539 540 541 542 543 544 545 546 547 548
	/**
	 * 获取月份起始日期
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMinMonthDate(String date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(famt.parse(date));
lal committed
549
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
ilal committed
550 551 552 553 554 555 556 557 558 559 560 561 562
		return famt.format(calendar.getTime());
	}

	/**
	 * 获取月份最后日期
	 * 
	 * @param date
	 * @return
	 * @throws ParseException
	 */
	public static String getMaxMonthDate(String date) throws ParseException {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(famt.parse(date));
lal committed
563
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
ilal committed
564 565
		return famt.format(calendar.getTime());
	}
lal committed
566

ilal committed
567 568
	/**
	 * 对比两个字符串数组
lal committed
569
	 * 
ilal committed
570 571 572 573
	 * @param t1
	 * @param t2
	 * @return
	 */
lal committed
574 575 576 577 578 579 580 581 582 583 584
	public static <T> List<T> compare(T[] t1, T[] t2) {
		List<T> list1 = Arrays.asList(t1); // 将t1数组转成list数组
		List<T> list2 = new ArrayList<T>();// 用来存放2个数组中不相同的元素
		for (T t : t2) {
			if (!list1.contains(t)) {
				list2.add(t);
			}
		}
		return list2;
	}

ilal committed
585
	public static String[] array_unique(String[] ss) {
lal committed
586
		Set<String> set = new HashSet<String>(Arrays.asList(ss));
ilal committed
587
		return set.toArray(new String[set.size()]);
lal committed
588
		// 或者return new HashSet<String>(Arrays.asList(ss)).toArray(new String[0]);
ilal committed
589 590
	}

lal committed
591 592 593
	public static byte[] CreateBSCommBufferFromString(String sCmdParam, byte[] bytCmd) {
		try {
			if (sCmdParam.length() == 0) {
ilal committed
594 595 596 597
				return bytCmd;
			}
			byte[] bytText = sCmdParam.getBytes("UTF-8");
			byte[] bytTextLen = int2byte(bytText.length + 1);
lal committed
598 599 600
			bytCmd = new byte[4 + bytText.length + 1];
			System.arraycopy(bytTextLen, 0, bytCmd, 0, bytTextLen.length);
			System.arraycopy(bytText, 0, bytCmd, 4, bytText.length);
ilal committed
601 602
			bytCmd[4 + bytText.length] = 0;
			return bytCmd;
lal committed
603
		} catch (Exception e) {
ilal committed
604
			e.printStackTrace();
lal committed
605
			bytCmd = new byte[0];
ilal committed
606 607 608
			return bytCmd;
		}
	}
lal committed
609 610 611

	public static byte[] int2byte(int res) {
		byte[] targets = new byte[4];
ilal committed
612 613 614 615
		targets[0] = (byte) (res & 0xff);
		targets[1] = (byte) ((res >> 8) & 0xff);
		targets[2] = (byte) ((res >> 16) & 0xff);
		targets[3] = (byte) (res >>> 24);
lal committed
616
		return targets;
ilal committed
617
	}
lal committed
618

ilal committed
619
	public static byte[] ConcateByteArray(byte[] abytDest, byte[] abytSrc) {
lal committed
620 621 622 623 624 625 626
		int len_dest = abytDest.length + abytSrc.length;
		if (abytSrc.length == 0)
			return abytDest;
		byte[] bytTmp = new byte[len_dest];
		System.arraycopy(abytDest, 0, bytTmp, 0, abytDest.length);
		System.arraycopy(abytSrc, 0, bytTmp, abytDest.length, abytSrc.length);
		return bytTmp;
ilal committed
627
	}
ilal committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656

	public static String dateToWeek2(String datetime) {

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
//        String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
		Calendar cal = Calendar.getInstance();
		Date date;
		try {
			date = f.parse(datetime);
			cal.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// 一周的第几天
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 
	 * @param nowDate   要比较的时间
	 * @param startDate 开始时间
	 * @param endDate   结束时间
	 * @return true在时间段内,false不在时间段内
	 * @throws Exception
	 */
lal committed
657
	public static boolean hourMinuteBetween(String nowDate, String startDate, String endDate, String date_format) {
ilal committed
658

lal committed
659
		SimpleDateFormat format = new SimpleDateFormat(date_format);
ilal committed
660

lal committed
661 662 663 664 665 666 667 668 669 670
		Date now = null;
		Date start = null;
		Date end = null;
		try {
			now = format.parse(nowDate);
			start = format.parse(startDate);
			end = format.parse(endDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
ilal committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

		long nowTime = now.getTime();
		long startTime = start.getTime();
		long endTime = end.getTime();

		return nowTime >= startTime && nowTime <= endTime;

	}

	/**
	 * 根据日期获取 星期 (2019-05-06 ——> 星期一)
	 * 
	 * @param datetime
	 * @return
	 */
	public static String dateToWeek(String datetime) {

		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
//        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
		String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
		Calendar cal = Calendar.getInstance();
		Date date;
		try {
			date = f.parse(datetime);
			cal.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// 一周的第几天
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

	/**
	 * 2019-10-24T00:30:23.000Z 转化时间
	 */
	public static String dealDateFormat(String oldDateStr, int num) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); // yyyy-MM-dd'T'HH:mm:ss.SSSZ
		Date date = null;
		try {
			date = df.parse(oldDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
		Date date1 = null;
		try {
			date1 = df1.parse(date.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		DateFormat df2 = null;
		if (num == 1) {
			df2 = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss");
		} else if (num == 2) {
			df2 = new SimpleDateFormat("HH:mm");
lal committed
729
		} else if (num == 3) {
lal committed
730
			df2 = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm");
ilal committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
		}

		return df2.format(date1);
	}

	/**
	 * 时间转换时间戳
	 */
	public static String dateToStamp(String s) {
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = simpleDateFormat.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long ts = date.getTime();
		res = String.valueOf(ts);
		return res;
	}
lal committed
752 753 754 755 756 757 758 759 760 761 762 763
	
	/**
	 * 时间戳转换时间
	 */
    public static String stampToDate(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
lal committed
764
    
lal committed
765
    
lal committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
    /**
	 * 时间戳转换时间
	 */
    public static String stampToDate2(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    
    /**
	 * 时间戳转换时间
	 */
    public static String stampToDate3(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
lal committed
789
    
lal committed
790 791 792 793 794 795 796 797 798 799 800 801
    /**
	 * 时间戳转换时间
	 */
    public static String stampToDate4(String s){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    
ilal committed
802 803 804 805 806 807 808 809 810 811 812 813
    /**
   	 * 时间戳转换时间
   	 */
   public static String stampToDate5(String s){
       String res;
       SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd HH:mm");
       long lt = new Long(s);
       Date date = new Date(lt);
       res = simpleDateFormat.format(date);
       return res;
   }
    
lal committed
814 815 816 817 818 819 820 821 822 823 824 825 826
    public static String Str_Time_formatting(String str,String Format) {
    	SimpleDateFormat sdf = new SimpleDateFormat(Format);
	       
//        String str = "2020-06-30 12:30:35"; // 把带时分秒的 date 转为 yyyy-MM-dd 格式的字符串
    	Date date2 = null;
        try {
            date2 = sdf.parse(str); // 把上面的字符串解析为日期类型
//            System.out.println(sdf.format(date2));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(date2);
    }
ilal committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
    
    
    /**
	 * 	Sun Nov 01 00:00:00 CST 2020
	 */
    public static String SunNovCST(String s){
    	String cst;
    	Date date = new Date(s);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		cst = simpleDateFormat.format(date);
        return cst;
    }
    
    /**
	 * 	Sun Nov 01 00:00:00 CST 2020
	 */
    public static String SunNovCSTYM(Date date2){
    	String cst;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
		cst = simpleDateFormat.format(date2);
        return cst;
    }
ilal committed
849 850 851 852 853 854 855 856 857 858
    
    /**
	 * 	Sun Nov 01 00:00:00 CST 2020
	 */
    public static String SunNovCSTYMD(Date date2){
    	String cst;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		cst = simpleDateFormat.format(date2);
        return cst;
    }
ilal committed
859 860 861 862 863 864 865 866 867 868
    
    /**
   	 * 	Sun Nov 01 00:00:00 CST 2020
   	 */
       public static String SunNovCSTY(Date date2){
       	String cst;
   		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
   		cst = simpleDateFormat.format(date2);
           return cst;
       }
ilal committed
869 870

}