计时

package os.system.digest.domain.basic;

import java.time.Instant;
import java.time.temporal.ChronoUnit;

/**
 * Instant,LocalDate,LocalTime 和 LocalDateTime
 * @since JDK_1.8
 * @see Product
 * @version 0.0.1
 */
public class TimeMillis {

	/**
	 * 只能精确到毫秒
	 * 常用计时
	 * @param start 在程序需要计时的地方标记:System.currentTimeMillis();
	 * @return
	 */
	public static long getCurrent(long start) {
		return System.currentTimeMillis() - start;
	}
	
	/**
	 * 常用计时获取毫秒
	 * @param start 在程序需要计时的地方标记:System.currentTimeMillis();
	 * @return
	 */
	public static Integer getCurrentMS(long start) {
		return (int) ((System.currentTimeMillis() - start)/1000/60);
	}
	
	/**
	 * 有更高的计时,可以精确到纳秒
	 * 新计时
	 * @since Java8
	 * @param now 在程序需要计时的地方标记:Instant now = Instant.now();
	 * @return
	 */
	public static long getUseTime(Instant now) {
		return ChronoUnit.NANOS.between(now, Instant.now());
	}
	
}

package os.system.digest.domain.basic;

import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import os.system.digest.factory.service.TargetFactoryService;

/**
 * <h3>Title:通用产品模板</h3><hr/>
 * <p>Description:Create By Juner On 12/6/2020</p>
 * @author Juner
 * @date 2020年12月6日 17点28分 星期日
 * @since JDK_1.8
 * @see Product
 * @version 0.0.1
 */
public abstract class Product<T extends Object> {
	
	/** 皮秒 */
	public final static String ps = "ps";
	
	/** 纳秒 */
	public final static String ns = "ns";
	
	/** 微妙 */
	public final static String us = "μs";
	
	/** 毫秒 */
	public final static String ms = "ms";
	
	/** 秒 */
	public final static String s = "s";
	
	/**
	 * 标记开始时间
	 */
	public Instant start;
	
	/**
	 * 标记结束时间
	 */
	public Instant end;
	
	/**
	 * 时间
	 */
	public long time;
	
	/**
	 * 构建实体类
	 * @return T 
	 */
	public abstract T builder();
	
	/**
	 * 睡眠
	 * @param ms 当前线程挂起指定的毫秒数
	 * @throws InterruptedException
	 */
	public final void sleep(int ms) throws InterruptedException {
		Thread.sleep(ms);
	}
	
	/**
	 * 开始计时
	 */
	public final void start() {
		this.start = Instant.now();
	}
	
	/**
	 * 结束计时
	 */
	public final void end() {
		this.end = Instant.now();
	}
	
	/**
	 * 获取纳秒级别的运行时间
	 * @return 纳秒
	 */
	public final String getNano() {
		return (this.end.getNano() - this.start.getNano()) + Product.ns;
	}
	
	/**
	 * 获取微秒级别的运行时间
	 * @return 微秒
	 */
	public final String getMicros() {
		return parseThousandthPercentile(getNano()) + Product.us;
	}
	
	/**
	 * 获取毫秒级别的运行时间
	 * @return 毫秒
	 */
	public final String getMillis() {
		return parseThousandthPercentile(parseThousandthPercentile(getNano())) + Product.ms;
	}
	
	/**
	 * 获取秒级别的运行时间
	 * @return 秒
	 */
	public final String getEpochSecond() {
		return (this.end.getEpochSecond() - this.start.getEpochSecond()) + Product.s;
	}

	/**
	 * 计算时长
	 * @since 需要再次之前运行 start 方法
	 * @return long
	 */
	public final long calc() {
		if(this.start != null)
			return 0;
		this.time = ChronoUnit.MILLIS.between(this.start, this.end);
		this.time = Duration.between(start, end).toMillis();
		return this.time;
	}
	
    /**
     * 计算分钟数
     * @param lastTime 开始时间
     * @param nextTime 结束时间
     * @return 分钟数
     */
    private final Integer getDuration(Date lastTime, Date nextTime){
        long s = (nextTime.getTime() - lastTime.getTime())/1000;
        return Integer.parseInt(String.valueOf(s/60));
    }

    /**
     * 千分位转换
     * @param number
     * @return
     */
    public final String parseThousandthPercentile(String number){
        String res;
        number = parseNumber(number);
        // 未判定异常情况,如双小数点的存在,逗号的存在,且只保留三位小数
        if (number.contains(".")) {
            int index = number.lastIndexOf(".");
            String[] data = number.split("\\.");
            res = data[0].substring(0, index - 3) + "." + data[0].substring(index - 3);
        } else {
            res = number.substring(0, number.length()-3) + "." + number.substring(number.length()-3);
        }
        return res;
    }

    /**
     * 只判断连续数字类型
     * @param number
     * @return
     */
    public final String parseNumber(String number) {
    	char[] array = number.toCharArray();
    	StringBuilder stringBuilder = new StringBuilder();
    	for(char c: array) {
    		if('0' == c
			|| '1' == c
			|| '2' == c
			|| '3' == c
			|| '4' == c
			|| '5' == c
			|| '6' == c
			|| '7' == c
			|| '8' == c
			|| '9' == c
			|| '.' == c) {
    	    	stringBuilder.append(c);
    		}
    	}
    	return stringBuilder.toString();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值