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();
        }
    }
}
原文地址:https://www.cnblogs.com/lwmp/p/10796050.html