博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
joda-time
阅读量:6734 次
发布时间:2019-06-25

本文共 4357 字,大约阅读时间需要 14 分钟。

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

* 依赖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
result1 = constructCycleTimeMap(ts, "hour"); for (Map.Entry
entry : result1.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } Map
result2 = constructCycleTimeMap(ts, "day"); for (Map.Entry
entry : result2.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } Map
result3 = constructCycleTimeMap(ts, "week"); for (Map.Entry
entry : result3.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } Map
result4 = constructCycleTimeMap(ts, "month"); for (Map.Entry
entry : result4.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } private static Map
constructCycleTimeMap(Long time, String sampling) throws ParseException { Map
map = new HashMap
(); 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

你可能感兴趣的文章
[激励机制]浅谈内部竞争——如何让你的员工玩命干活?
查看>>
【working_out】时间线
查看>>
C++ 中 volatile 的使用
查看>>
ubuntu下安装jdk
查看>>
php常见的类库-文件操作类
查看>>
python3创建目录
查看>>
Get Main Thread ID
查看>>
JavaScript + HTML 经典多页面组织方式
查看>>
oracle instantclient + plsql 远程连接数据库
查看>>
dede调用img图片
查看>>
LeetCode——Invert Binary Tree
查看>>
LeetCode - 703. Kth Largest Element in a Stream
查看>>
vue.js中引入图片
查看>>
A damn at han’s Windows phone book 笔记(6:page navigation & data binding)
查看>>
C# PDF 全攻略
查看>>
android第二次作业
查看>>
java流类基础练习。
查看>>
kill-9 kill-3
查看>>
登陆密码加密
查看>>
MVC过滤器
查看>>