Search in sources :

Example 6 with TimeInterval

use of cn.hutool.core.date.TimeInterval in project ballcat by ballcat-projects.

the class AbstractPunctuator method punctuate.

@Override
public void punctuate(long timestamp) {
    try {
        if (isHandle()) {
            TimeInterval interval = new TimeInterval();
            startLog();
            handle(timestamp);
            endLog(interval.intervalMs());
            // 清除数据
            clean();
            context.commit();
        }
    } catch (Exception e) {
        errLog(e);
    }
}
Also used : TimeInterval(cn.hutool.core.date.TimeInterval)

Example 7 with TimeInterval

use of cn.hutool.core.date.TimeInterval in project ballcat by ballcat-projects.

the class AbstractNoticeGlobalExceptionHandler method run.

@Override
@SuppressWarnings("all")
public void run() {
    String key;
    TimeInterval interval = new TimeInterval();
    long threadId = Thread.currentThread().getId();
    while (true) {
        int i = 0;
        while (i < config.getMax() && interval.intervalSecond() < config.getTime()) {
            Throwable t = null;
            try {
                // 如果 i=0,即 当前未处理异常,则等待超时时间为 1 小时, 否则为 10 秒
                t = queue.poll(i == 0 ? TimeUnit.HOURS.toSeconds(1) : 10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (t != null) {
                key = t.getMessage() == null ? NULL_MESSAGE_KEY : t.getMessage();
                // i++
                if (i++ == 0) {
                    // 第一次收到数据, 重置计时
                    interval.restart();
                    messages.put(key, init(t).setKey(key).setThreadId(threadId));
                } else {
                    if (messages.containsKey(key)) {
                        messages.put(key, messages.get(key).increment());
                    } else {
                        messages.put(key, init(t).setKey(key).setThreadId(threadId));
                    }
                }
            }
        }
        // 一次处理结束
        if (messages.size() > 0) {
            // 如果需要发送的消息不为空
            messages.forEach((k, v) -> {
                try {
                    ExceptionNoticeResponse response = send(v);
                    if (!response.isSuccess()) {
                        log.error("消息通知发送失败! msg: {}", response.getErrMsg());
                    }
                } catch (Exception e) {
                    log.error("消息通知时发生异常", e);
                }
            });
            messages.clear();
        }
        interval.restart();
    }
}
Also used : TimeInterval(cn.hutool.core.date.TimeInterval) ExceptionNoticeResponse(com.hccake.ballcat.autoconfigure.web.exception.domain.ExceptionNoticeResponse)

Example 8 with TimeInterval

use of cn.hutool.core.date.TimeInterval in project springboot-templet-start by thedestiny.

the class HutoolTestCase method test003.

/**
 * 时间相关
 * DateUtil.parse方法会自动识别一些常用格式,包括:
 * yyyy-MM-dd HH:mm:ss
 * yyyy-MM-dd
 * HH:mm:ss
 * yyyy-MM-dd HH:mm
 * yyyy-MM-dd HH:mm:ss.SSS
 */
@Test
public void test003() {
    // 当前时间
    Date date = DateUtil.date();
    // 当前时间
    Date date2 = DateUtil.date(Calendar.getInstance());
    // 当前时间
    Date date3 = DateUtil.date(System.currentTimeMillis());
    // 当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
    String now = DateUtil.now();
    // 当前日期字符串,格式:yyyy-MM-dd
    String today = DateUtil.today();
    String dateStr = "2017-03-01";
    Date date5 = DateUtil.parse(dateStr);
    String dateStr1 = "2017-03-01";
    Date date6 = DateUtil.parse(dateStr1, "yyyy-MM-dd");
    String dateStr3 = "2017-03-01";
    Date date7 = DateUtil.parse(dateStr);
    // 结果 2017/03/01
    String format = DateUtil.format(date, "yyyy/MM/dd");
    // 常用格式的格式化,结果:2017-03-01
    String formatDate = DateUtil.formatDate(date);
    // 结果:2017-03-01 00:00:00
    String formatDateTime = DateUtil.formatDateTime(date);
    // 结果:00:00:00
    String formatTime = DateUtil.formatTime(date);
    // 获得年的部分
    DateUtil.year(date);
    // 获得月份,从0开始计数
    DateUtil.month(date);
    // 获得月份枚举
    DateUtil.monthEnum(date);
    String dateStr4 = "2017-03-01 22:33:23";
    Date date8 = DateUtil.parse(dateStr4);
    /**
     *  可以获取 一分的开始 一个小时的开始 一天 一周 一月 一个季度 一年
     */
    // 一天的开始,结果:2017-03-01 00:00:00
    Date beginOfDay = DateUtil.beginOfDay(date8);
    // 一天的结束,结果:2017-03-01 23:59:59
    Date endOfDay = DateUtil.endOfDay(date8);
    String dateStr7 = "2017-03-01 22:33:23";
    Date date9 = DateUtil.parse(dateStr7);
    // 结果:2017-03-03 22:33:23
    Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
    // 常用偏移,结果:2017-03-04 22:33:23
    DateTime newDate2 = DateUtil.offsetDay(date, 3);
    // 常用偏移,结果:2017-03-01 19:33:23
    DateTime newDate3 = DateUtil.offsetHour(date, -3);
    // 昨天
    DateUtil.yesterday();
    // 明天
    DateUtil.tomorrow();
    // 上周
    DateUtil.lastWeek();
    // 下周
    DateUtil.nextWeek();
    // 上个月
    DateUtil.lastMonth();
    // 下个月
    DateUtil.nextMonth();
    String dateStr11 = "2017-03-01 22:33:23";
    Date date11 = DateUtil.parse(dateStr11);
    String dateStr2 = "2017-04-01 23:33:23";
    Date date12 = DateUtil.parse(dateStr2);
    // 相差一个月,31天
    long betweenDay = DateUtil.between(date11, date12, DateUnit.DAY);
    // Level.MINUTE表示精确到分
    // String formatBetween = DateUtil.formatBetween(date11, date12, BetweenFormater.Level.MINUTE);
    TimeInterval timer = DateUtil.timer();
    // ---------------------------------
    // -------这是执行过程
    // ---------------------------------
    // 花费毫秒数
    timer.interval();
    // 返回花费时间,并重置开始时间
    timer.intervalRestart();
    // 花费分钟数
    timer.intervalMinute();
    DateTime dateTime = new DateTime("2017-01-05 12:34:23", DatePattern.NORM_DATETIME_FORMAT);
    // 结果:2017-01-05 12:34:23
    String dateStr33 = dateTime.toString();
    // 年,结果:2017
    int year = dateTime.year();
    // 季度(非季节),结果:Season.SPRING
    Quarter season = dateTime.quarterEnum();
    // 月份,结果:Month.JANUARY
    Month month = dateTime.monthEnum();
    // 日,结果:5
    int day = dateTime.dayOfMonth();
    // 默认情况下DateTime为可变对象,此时offsite == dateTime
    DateTime offsite = dateTime.offset(DateField.YEAR, 0);
    // 设置为不可变对象后变动将返回新对象,此时offsite != dateTime
    dateTime.setMutable(false);
    offsite = dateTime.offset(DateField.YEAR, 0);
}
Also used : Quarter(cn.hutool.core.date.Quarter) Month(cn.hutool.core.date.Month) TimeInterval(cn.hutool.core.date.TimeInterval) Date(java.util.Date) DateTime(cn.hutool.core.date.DateTime) Test(org.junit.Test)

Example 9 with TimeInterval

use of cn.hutool.core.date.TimeInterval in project springboot-templet-start by thedestiny.

the class HttpUtils method main.

public static void main(String[] args) throws InterruptedException {
    TimeInterval timer = DateUtil.timer();
    System.out.println("start ");
    // ---------------------------------
    // -------这是执行过程
    // ---------------------------------
    TimeUnit.SECONDS.sleep(2);
    System.out.println("end ");
    // 花费毫秒数
    System.out.println(timer.interval());
    // 花费毫秒数
    System.out.println(timer.intervalSecond());
}
Also used : TimeInterval(cn.hutool.core.date.TimeInterval)

Example 10 with TimeInterval

use of cn.hutool.core.date.TimeInterval in project hutool by looly.

the class HttpRequestTest method asyncGetTest.

@Test
@Ignore
public void asyncGetTest() {
    TimeInterval timer = DateUtil.timer();
    HttpResponse body = HttpRequest.get(url).charset("GBK").executeAsync();
    long interval = timer.interval();
    timer.restart();
    Console.log(body.body());
    long interval2 = timer.interval();
    Console.log("Async response spend {}ms, body spend {}ms", interval, interval2);
}
Also used : TimeInterval(cn.hutool.core.date.TimeInterval) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TimeInterval (cn.hutool.core.date.TimeInterval)18 Test (org.junit.Test)14 Ignore (org.junit.Ignore)12 HttpResponse (cn.hutool.http.HttpResponse)2 DateTime (cn.hutool.core.date.DateTime)1 Month (cn.hutool.core.date.Month)1 Quarter (cn.hutool.core.date.Quarter)1 JSONObject (com.alibaba.fastjson.JSONObject)1 ExceptionNoticeResponse (com.hccake.ballcat.autoconfigure.web.exception.domain.ExceptionNoticeResponse)1 Date (java.util.Date)1 BoundSql (org.apache.ibatis.mapping.BoundSql)1 MappedStatement (org.apache.ibatis.mapping.MappedStatement)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1 Configuration (org.apache.ibatis.session.Configuration)1