当前位置:   article > 正文

java验证接口时间戳是否过期_java 验证时间戳有效期

java 验证时间戳有效期

代码

//计算日期差,不超过5分钟
long diff = Math.abs(CommonDateUtil.diff(new Date(), time, CommonDateUtil.MINUTE_MS));
		if (diff < 0 || diff > 5) {
			return "已过期,请更新时间戳";
		}
  • 1
  • 2
  • 3
  • 4
  • 5

工具

package org.springblade.modules.api.utils;

import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 通用工具类
 *
 * @author Chill
 */
public class CommonDateUtil {
	private  final static String FIRST="1";
	private final  static String SECOND="2";
	private final  static String THIRD="3";
	private final static String FOUR="4";

	/** 毫秒 */
	private final static long MS = 1;
	/** 每秒钟的毫秒数 */
	private final static long SECOND_MS = MS * 1000;
	/** 每分钟的毫秒数 */
	public final static long MINUTE_MS = SECOND_MS * 60;



	/**
	 * 不够位数的在前面补0,保留num的长度位数字
	 *
	 * @param code 码
	 * @return 编号
	 */
	public static String autoGenericCode(String code, int num) {
		String result = "";
		result = String.format("%0" + num + "d", Integer.parseInt(code));
		return result;
	}


	/**
	 * 获取季度
	 * @param date 当前日期
	 * @return 季度
	 * @throws ParseException
	 */
	public static String getQuarter(LocalDateTime date) throws ParseException {
		if (Func.isEmpty(date)) {
			throw new RuntimeException("日期不能为空");
		}
		String quarter="";
		int month= Func.toInt(DateUtil.format(date,"M"));
		if(month>=1&&month<=3){
			quarter= date.getYear()+FIRST;
		}
		if(month>=4&&month<=6){
			quarter= date.getYear()+SECOND;
		}
		if(month>=7&&month<=9){
			quarter= date.getYear()+THIRD;
		}
		if(month>=10&&month<=12){
			quarter=date.getYear()+FOUR;
		}
		return quarter;
	}



	/**
	 * 获取百分比
	 * @param num1
	 * @param num2
	 * @return
	 */
	public static String TransPercent(int num1,int num2){
		DecimalFormat df =new DecimalFormat();
		df.setMaximumFractionDigits(1);
		df.setMinimumFractionDigits(1);
		if(num2==0){
			return "0%";
		}else{
			String percent = df.format(num1 *100.0 / num2) +"%";
			return percent;
		}
	}



	/**
	 * 字符串转换数字
	 *
	 * @param id
	 * @return
	 */
	public static Integer parseInt(String id) {
		Integer uId = 0;

		if (id == null || "".equals(id))
			return -1;

		try {
			uId = Integer.parseInt(id);
		} catch (NumberFormatException e) {
			// e.printStackTrace();
			uId = -1;
		}

		return uId;
	}

	/**
	 * 解析前台传来的数据。格式: 1,2,3
	 *
	 * @param param
	 * @return
	 */
	public static Integer[] parseParmas(String param) {
		Objects.requireNonNull(param);

		Integer[] idList = null;

		try {
			String id = URLDecoder.decode(param, "utf-8");
			Objects.requireNonNull(id);

			String[] ids = id.split(",");
			int len = ids.length;
			idList = new Integer[len];
			for (int i = 0; i < len; i++) {
				idList[i] = Integer.parseInt(ids[i].trim());
			}
		} catch (NumberFormatException | UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return idList;
	}

	/**
	 * 非空转换
	 *
	 * @param value
	 * @return
	 */
	public static String noEmpty(Object value) {
		return Optional.ofNullable(value)
				.map(v -> v.toString())
				.orElse("");
	}

	/**
	 * 非空转换
	 *
	 * @param value
	 * @return
	 */
	public static Integer noEmpty(Integer value) {
		return value == null ? 0 : value;
	}

	/**
	 * LocalDateTime 转换为 Date
	 *
	 * @param localDateTime
	 * @return
	 */
	public static Date LocalDateTimeToDate(LocalDateTime localDateTime) {
		ZoneId zone = ZoneId.systemDefault();
		Instant instant = localDateTime.atZone(zone).toInstant();
		return Date.from(instant);
	}

	/**
	 * Date 转换为 LocalDateTime
	 *
	 * @param date
	 * @return
	 */
	public static LocalDateTime DateToLocalDateTime(Date date) {
		Instant instant = date.toInstant();
		ZoneId zone = ZoneId.systemDefault();
		return LocalDateTime.ofInstant(instant, zone);
	}

	/**
	 *
	 * @param dateTime
	 * @return
	 */
	public static String parseDateStr(LocalDateTime dateTime) {
		Objects.requireNonNull(dateTime);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = LocalDateTimeToDate(dateTime);
		return sdf.format(date);
	}

	/**
	 * 解析字符串格式的时间
	 * @param date
	 * @return
	 */
	public static Map<String, Object> parseDateStr(String date) {
		Objects.requireNonNull(date);

		Map<String, Object> dateMap = new HashMap<>();
		String[] values = date.split(",");
		if (values != null && values.length == 2) {
			String begin = values[0].trim();
			if (begin != null && begin.length() >= 10)
				begin = begin.substring(0, 10);
			dateMap.put("begin", begin);

			String end = values[1].trim();
			if (end != null && end.length() >= 10)
				end = end.substring(0, 10);
			dateMap.put("end", end);
		}
		return dateMap;
	}

	/**
	 * 解析逗号分隔的日期
	 *
	 * @param date
	 * @param isDate
	 * @return
	 */
	public static Map<String, Object> parseDate(String date, boolean isDate) {
		Map<String, Object> dateMap = new HashMap<>();
		if(date != null) {
			String[] values = date.split(",");
			if (values != null && values.length == 2) {
				String begin = values[0].trim();
				String end = values[1].trim();
				if (isDate) {
					try {
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						Date beginDate = sdf.parse(values[0]);
						Date endDate = sdf.parse(values[1]);
						//结束日期加一天
						Calendar calendar = Calendar.getInstance();
						calendar.setTime(endDate);
						calendar.add(Calendar.DATE, 1);
						endDate = calendar.getTime();
						dateMap.put("begin", DateToLocalDateTime(beginDate));
						dateMap.put("end", DateToLocalDateTime(endDate));
					} catch (ParseException e) {
						e.printStackTrace();
					}
				} else {
					dateMap.put("begin", begin);
					dateMap.put("end", end);
				}
			}
		}
		return dateMap;
	}

	/**
	 * 获取对象的所有属性
	 *
	 * @param obj
	 * @return
	 */
	public static Field[] getFields(Object obj) {
		return getFields(obj, false);
	}

	/**
	 * 获取对象的所有属性,包括从父类继承的属性
	 *
	 * @param obj
	 * @param parent
	 * @return
	 */
	public static Field[] getFields(Object obj, boolean parent) {
		Objects.requireNonNull(obj);

		List<Field> fieldList = new ArrayList<>();
		Class<? extends Object> clazz = obj.getClass();

		while (clazz != null) {
			fieldList.addAll(new ArrayList<>(Arrays.asList(clazz.getDeclaredFields())));
			clazz = parent ? clazz.getSuperclass() : null;
		}

		Field[] fields = new Field[fieldList.size()];
		return fieldList.toArray(fields);
	}

	/**
	 * 获取通用对象字段的值
	 *
	 * @param object
	 * @param fieldName
	 * @return
	 * @throws NoSuchMethodException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static Object getFieldValue(Object object, String fieldName)
			throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
		Objects.requireNonNull(object);

		Object value = null;
		String getName = capitalize(fieldName);
		if (notEmpty(getName)) {
			Method m = object.getClass().getMethod("get" + getName);
			value = m.invoke(object);
		}
		return value;
	}

	/**
	 * 验证字符串是否为空
	 *
	 * @param value
	 * @return
	 */
	public static boolean notEmpty(String value) {
		return value != null && !"".equals(value);
	}

	/**
	 * 首字母转换为大写
	 *
	 * @param fieldName
	 * @return
	 */
	public static String capitalize(String fieldName) {
		String getName = "";
		if (notEmpty(fieldName)) {
			getName = fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
		}

		return getName;
	}

	/**
	 * 从字符串开始处抽取指定长度的字符
	 *
	 * @param content
	 * @param len
	 * @return
	 */
	public static String extract(String content, int len) {
		return Optional.ofNullable(content)
				.filter(c -> c.length() > len)
				.map(c -> {
					StringBuffer buffer = new StringBuffer();
					buffer.append(c.substring(0, len)).append("...");
					return buffer.toString();
				}).orElse(content);
	}

	/**
	 * 字符串数据转为数字数组
	 * @param str
	 * @return
	 */
	public static Integer[] str2IntArray(String str) {
		String[] strs = str.split(",");
		Integer[] original = new Integer[strs.length];
		for (int i = 0, len = strs.length; i < len; i++) {
			original[i] = parseInt(strs[i]);
		}
		return original;
	}

	/**
	 * 计算占比
	 * @param num
	 * @param total
	 * @return
	 */
	public static Float calcPercent(int num, int total) {
		Float rate = (num * 10000.0f) / total;
		return ((new BigDecimal(rate).setScale(0, BigDecimal.ROUND_HALF_UP)).floatValue()) / 10000;
	}

	/**
	 * Check list not null
	 * @param list
	 * @return
	 */
	public static boolean validList(List<?> list) {
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	public static String deleteAllHTMLTag(String source) {
		if (source == null) {
			return "";
		}

		String s = source;
		/** 删除普通标签 */
		s = s.replaceAll("<(S*?)[^>]*>.*?|<.*? />", "");
		/** 删除转义字符 */
		s = s.replaceAll("&.{2,6}?;", "");

		return s;
	}

	public static Map<String, Map<String, Object>> mapAscSortedByKey(Map<String, Map<String, Object>> unsortMap) {
		Map<String, Map<String, Object>> result = new HashMap<>();

		if (unsortMap != null) {
			unsortMap.entrySet().stream().sorted(Map.Entry.<String, Map<String, Object>>comparingByKey())
					.forEachOrdered(x -> result.put(x.getKey(), x.getValue()));
		}

		return result;
	}

	public static Date strToDate(String dateStr) {
		return strToDate(dateStr, "yyyy-MM-dd");
	}

	public static Date strToDate(String dateStr, String format) {
		Date date = new Date();
		if (dateStr != null) {
			try {
				SimpleDateFormat sdf = new SimpleDateFormat(format);
				date = sdf.parse(dateStr);
			} catch (ParseException e) {
				date = new Date();
			}
		}
		return date;
	}

	/**
	 *
	 * @param date
	 * @return
	 */
	public static String parseDateStr(Date date) {
		return parseDateStr(date, "yyyy-MM-dd");
	}

	public static String parseDateStr(Date date, String format) {
		String dateStr = "";
		if (date != null) {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			dateStr = sdf.format(date);
		}
		return dateStr;
	}

	public static double calcRate(long num, long total) {
		double rate = 0;
		if (num != 0) {
			try {
				double temp = (float) (num * 100) / total;
				BigDecimal b = new BigDecimal(temp);
				rate = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			} catch (Exception e) {

			}
		}

		return rate;
	}

	public static String calcPerc(long num, long total) {
		String result = "0";
		if (total != 0) {
			if (num > total) {
				num = total;
			}

			try {
				// 创建一个数值格式化对象
				NumberFormat numberFormat = NumberFormat.getInstance();
				// 设置精确到小数点后2位
				numberFormat.setMaximumFractionDigits(2);
				result = numberFormat.format((float) num / (float) total * 100);
			} catch (Exception e) {
			}
		}

		return result + "%";
	}


    public  static  int objToInt(Object obj){
		if(obj==null){
			return 0;
		}

		if(obj instanceof Double){
			return  ((Double) obj).intValue();
		}
		if(obj instanceof  BigDecimal){
			return  ((BigDecimal) obj).intValue();
		}
		return  Integer.parseInt(obj.toString());
	};



	/**
	 * 等待线程结束
	 * @param workers
	 */
	public static  void joinThread(List<Thread> workers){
		//等待线程结束
		workers.forEach(e->{
			try {
				e.join();
			}catch (Exception ex){
				ex.printStackTrace();
			}finally {
				e.interrupt();
			}
		});

	}

	/**
	 * 计算日期差
	 * @param subtrahend
	 * @param minuend
	 * @param diffField
	 * @return
	 */
	public static long diff(Date subtrahend, Date minuend, long diffField) {
		long diff = minuend.getTime() - subtrahend.getTime();
		return diff / diffField;
	}
	/**
	 * 将时间戳变为日期
	 * @param s
	 * @return
	 */
	public static Date 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 DateUtil.parse(res, DateUtil.PATTERN_DATETIME);
	}




}

  • 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
  • 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
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/119314
推荐阅读
相关标签
  

闽ICP备14008679号