• joda-time


    # 最近写了一些有意思的需求,用了一些有意思的工具

    * 依赖joda-time

    * 需求1: 任意给定时间t, 求t所在的时间点的所在小时,所在天,所在周,所在月的开始时间到结束时间的timestamp(有点绕,估计看代码更容易理解)

    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.joda.time.DateTime;
    
    public class QQ {
    
        public static void main(String[] args) throws ParseException {
            Long ts = 1556606641000L;
            Map<String, Object> result1 = constructCycleTimeMap(ts, "hour");
            for (Map.Entry<String, Object> entry : result1.entrySet()) {
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }
    
            Map<String, Object> result2 = constructCycleTimeMap(ts, "day");
            for (Map.Entry<String, Object> entry : result2.entrySet()) {
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }
    
            Map<String, Object> result3 = constructCycleTimeMap(ts, "week");
            for (Map.Entry<String, Object> entry : result3.entrySet()) {
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }
    
            Map<String, Object> result4 = constructCycleTimeMap(ts, "month");
            for (Map.Entry<String, Object> entry : result4.entrySet()) {
                System.out.println(entry.getKey() + ": " + entry.getValue());
            }
        }
    
        private static Map<String, Object> constructCycleTimeMap(Long time, String sampling) throws ParseException {
            Map<String, Object> map = new HashMap<String, Object>();
            if (SamplingEnum.HOUR.getName().equals(sampling)) {
                DateTime date = new DateTime(time);
                Long ts = date.minuteOfHour().withMinimumValue().secondOfMinute().withMinimumValue().getMillis();
                map.put("start", ts);
                map.put("start2", timestampToStr(ts, "yyyy-MM-dd HH:mm:ss"));
    
                ts = date.minuteOfHour().withMaximumValue().secondOfMinute().withMaximumValue().getMillis();
                map.put("end", ts);
                map.put("end2", timestampToStr(ts, "yyyy-MM-dd HH:mm:ss"));
            } else if (SamplingEnum.DAY.getName().equals(sampling)) {
                DateTime date = new DateTime(time);
                Long ts = date.hourOfDay().withMinimumValue().minuteOfHour().withMinimumValue().secondOfMinute()
                        .withMinimumValue().getMillis();
                map.put("start", ts);
                map.put("start2", timestampToStr(ts, "yyyy-MM-dd HH:mm:ss"));
    
                ts = date.hourOfDay().withMaximumValue().minuteOfHour().withMaximumValue().secondOfMinute()
                        .withMaximumValue().getMillis();
                map.put("end", ts);
                map.put("end2", timestampToStr(ts, "yyyy-MM-dd HH:mm:ss"));
            } else if (SamplingEnum.WEEK.getName().equals(sampling)) {
                // 使用jode time 获取当前所在星期的星期一
                DateTime date = new DateTime(time);
                Long ts = date.dayOfWeek().withMinimumValue().hourOfDay().withMinimumValue().minuteOfHour()
                        .withMinimumValue().secondOfMinute().withMinimumValue().getMillis();
                map.put("start", ts);
                map.put("start2", timestampToStr(ts, "yyyy-MM-dd HH:mm:ss"));
    
                // 使用jode time 获取当前所在星期的星期日
                ts = date.dayOfWeek().withMaximumValue().hourOfDay().withMaximumValue().minuteOfHour().withMaximumValue()
                        .secondOfMinute().withMaximumValue().getMillis();
                map.put("end", ts);
                map.put("end2", timestampToStr(ts, "yyyy-MM-dd HH:mm:ss"));
            } else if (SamplingEnum.MONTH.getName().equals(sampling)) {
                DateTime date = new DateTime(time);
                Long ts = date.dayOfMonth().withMinimumValue().hourOfDay().withMinimumValue().minuteOfHour()
                        .withMinimumValue().secondOfMinute().withMinimumValue().getMillis();
                map.put("start", ts);
                map.put("start2", timestampToStr(ts, "yyyy-MM-dd HH:mm:ss"));
    
                ts = date.dayOfMonth().withMaximumValue().hourOfDay().withMaximumValue().minuteOfHour().withMaximumValue()
                        .secondOfMinute().withMaximumValue().getMillis();
                map.put("end", ts);
                map.put("end2", timestampToStr(ts, "yyyy-MM-dd HH:mm:ss"));
            }
    
            return map;
        }
    
        private static String timestampToStr(Long timestamp, String formatStr) {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            Date date = new Date(timestamp);
            return format.format(date);
        }
    
        public enum SamplingEnum {
    
            HOUR("hour"), DAY("day"), WEEK("week"), MONTH("month");
    
            private String name;
    
            private SamplingEnum(String name) {
                this.name = name;
            }
    
            public String getName() {
                return name;
            }
    
            public static Boolean contains(String sampling) {
                Boolean isContained = false;
                for (SamplingEnum item : values()) {
                    if (item.getName().equals(sampling)) {
                        isContained = true;
                    }
                }
                return isContained;
            }
    
            public static String toStr() {
                StringBuilder str = new StringBuilder();
                for (SamplingEnum item : values()) {
                    str.append(item.getName() + ", ");
                }
                return str.toString();
            }
        }
    }
  • 相关阅读:
    bzoj 2527: [Poi2011]Meteors 整体二分
    bzoj 2738 矩阵乘法
    bzoj 3110 K大数查询
    bzoj 3262 陌上花开
    cogs 577 蝗灾 CDQ分治
    bzoj 1101 zap
    bzoj 2005
    bzoj 3518 Dirichlet卷积
    bzoj 1257
    最优贸易 [NOIP 2009]
  • 原文地址:https://www.cnblogs.com/lwmp/p/10796050.html
Copyright © 2020-2023  润新知