Java 关于时间 做一些笔记 方便下一次使用

获取UTC时间

	/**
	 * 获取UTC时间
	 *
	 * @return
	 */
	public static final String currentUtcTime() {
		LocalDateTime now = LocalDateTime.now(ZoneOffset.UTC);
		return now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
	}

获取当前时间

	/**
	 * 获取当前时间
	 *
	 * @return
	 */
	public static final String getTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(System.currentTimeMillis());
		return formatter.format(date);
	}

对时间做增加操作

	/**
	 * 给当前时间加2个月
	 *
	 * @return
	 */
	public static final String getAddTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date(System.currentTimeMillis());
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONDAY, 2);
		return formatter.format(cal.getTime());
	}

当前时间在 开始时间 结束时间 之间

	/**
	 * 当前时间在 开始时间 结束时间 之间  ,相当于一个活动是否过期
	 * @param statime
	 * @param endTime
	 * @return
	 */
	public static final Boolean getQualifications(String statime, String endTime) {
		log.info("开始对时间做处理 开始时间{},------结束时间{},----------", statime, endTime);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
		try {
			Date staData = formatter.parse(statime);
			Date endData = formatter.parse(endTime);
			Date newData = formatter.parse(LocalDateTime.now(ZoneOffset.UTC) + "");
			// 开始时间比当前时间小	结束时间比当前时间大
			if (newData.after(staData) || endData.after(newData)) {
				return true;
			} else {
				return false;
			}
		} catch (ParseException e) {
			log.info("转换时间异常{}", e);
			e.printStackTrace();
		}
		return true;
	}
/**
	 * 判断用户是否是最近3天注册的 1是 0否
	 *
	 * @param time
	 * @return
	 */
	public static final int changeUserCreateTimeIsRecent(String time) {
		log.info("进入判断注册时间是否最近注册的用户注册时间 :{}", time);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String newTime = TimeUtil.getTime(); // 这个方法是获取的当前时间
		log.info("当前时间{}", newTime);
		try {
			Date newdata = dateFormat.parse(newTime);
			Date createData = dateFormat.parse(time);
			long l = newdata.getTime() - createData.getTime();
			log.info("时间比较剩余时间:{}", l);
			long days = l / (1000 * 60 * 60 * 24);
			log.info("转为天数为:{}", days);
			if (days <= 3) {
				return 1;
			} else {
				return 0;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	int i = ((int) (10 * Math.random())) % 2;  // 随机 (0 1)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

唐洋QuQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值