admin 管理员组文章数量: 887021
JAVA Calendar计算 昨天,今天,上周,本周,上个月,本月,上个季度,本季度,近半年,上半年,下半年,上一年,今年
1.计算时间工具类
package com.trs.quwei;/*** Calendar计算 昨天,今天,上周,本周,上个月,本月,上个季度,本季度,近半年,上半年,下半年,上一年,今年** @Author quwei* @Date 2019/09/11*/import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;public class DateUtil {/*** 获取今日开始时间和结束时间** @param condition* @return*/public static void setDayRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.MILLISECOND, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.HOUR_OF_DAY, 0);condition.put("startTime", calendar.getTime());calendar.set(Calendar.HOUR_OF_DAY, 24);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** 获取昨天开始时间和结束时间** @param condition* @return*/public static void setYesterdayRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.MILLISECOND, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.HOUR_OF_DAY, -24);condition.put("startTime", calendar.getTime());calendar.set(Calendar.MILLISECOND, 0);calendar.set(Calendar.SECOND, 59);calendar.set(Calendar.MINUTE, 59);calendar.set(Calendar.HOUR_OF_DAY, 23);condition.put("stopTime", calendar.getTime());}/*** 获得最近三天的开始时间和结束时间** @param condition* @return*/public static void setThreeRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.MILLISECOND, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.HOUR_OF_DAY, 24);condition.put("stopTime", calendar.getTime());calendar.set(Calendar.HOUR_OF_DAY, -72);condition.put("startTime", calendar.getTime());}/*** 获得近一周的开始时间和结束时间** @param condition* @return*/public static void setDaySevenRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.HOUR_OF_DAY, 24);condition.put("stopTime", calendar.getTime());calendar.set(Calendar.HOUR_OF_DAY, -168);condition.put("startTime", calendar.getTime());}/*** 获得近一月的开始时间和结束时间** @param condition* @return*/public static void setDayTRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.HOUR_OF_DAY, 24);condition.put("stopTime", calendar.getTime());calendar.set(Calendar.HOUR_OF_DAY, -720);condition.put("startTime", calendar.getTime());}/*** 获取本周开始时间和结束时间** @param condition* @return*/public static void setWeekRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);condition.put("startTime", calendar.getTime());calendar.add(Calendar.DAY_OF_WEEK, 7);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** 获取上周开始时间和结束时间** @param condition* @return*/public static void setLastWeekRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);calendar.add(Calendar.DAY_OF_WEEK, -7);condition.put("startTime", calendar.getTime());calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);calendar.add(Calendar.DAY_OF_WEEK, 6);condition.put("stopTime", calendar.getTime());}/*** 获取本月开始时间和结束时间** @param condition* @return*/public static void setMonthRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));condition.put("startTime", calendar.getTime());calendar.add(Calendar.DAY_OF_WEEK, 7);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// calendar.set(Calendar.HOUR_OF_DAY, 24);calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);condition.put("stopTime", calendar.getTime());}/*** 获取上月开始时间和结束时间** @param condition* @return*/public static void setLastMonthRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.add(Calendar.MONTH, -1);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));condition.put("startTime", calendar.getTime());calendar.add(Calendar.DAY_OF_WEEK, 7);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));//calendar.set(Calendar.HOUR_OF_DAY, 24);calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);condition.put("stopTime", calendar.getTime());}/*** 获取本季度开始时间和结束时间** @param condition* @return*/public static void setQuarterRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);int currentMonth = calendar.get(Calendar.MONTH) + 1;try {if (currentMonth >= 1 && currentMonth <= 3)calendar.set(Calendar.MONTH, 0);else if (currentMonth >= 4 && currentMonth <= 6)calendar.set(Calendar.MONTH, 3);else if (currentMonth >= 7 && currentMonth <= 9)calendar.set(Calendar.MONTH, 6);else if (currentMonth >= 10 && currentMonth <= 12)calendar.set(Calendar.MONTH, 9);calendar.set(Calendar.DATE, 1);} catch (Exception e) {e.printStackTrace();}condition.put("startTime", calendar.getTime());calendar.add(Calendar.MONTH, 3);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** 获取上季度开始时间和结束时间** @param condition* @return*/public static void setLastQuarterRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);int currentMonth = calendar.get(Calendar.MONTH) + 1;try {if (currentMonth >= 1 && currentMonth <= 3)calendar.set(Calendar.MONTH, 0);else if (currentMonth >= 4 && currentMonth <= 6)calendar.set(Calendar.MONTH, 3);else if (currentMonth >= 7 && currentMonth <= 9)calendar.set(Calendar.MONTH, 6);else if (currentMonth >= 10 && currentMonth <= 12)calendar.set(Calendar.MONTH, 9);calendar.set(Calendar.DATE, 1);} catch (Exception e) {e.printStackTrace();}calendar.add(Calendar.MONTH, -3);condition.put("startTime", calendar.getTime());calendar.add(Calendar.MONTH, 3);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** 获取近半年开始时间和结束时间** @param condition* @return*/public static void setHalfYearRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));calendar.add(Calendar.MONTH, -6);condition.put("startTime", calendar.getTime());calendar.add(Calendar.MONTH, 6);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** 获取上半年开始时间和结束时间** @param condition* @return*/public static void setUpHalfYearRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));condition.put("startTime", calendar.getTime());calendar.add(Calendar.MONTH, 6);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** 获取下半年开始时间和结束时间** @param condition* @return*/public static void setNextHalfYearRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));calendar.add(Calendar.MONTH, 6);condition.put("startTime", calendar.getTime());calendar.add(Calendar.MONTH, 6);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** 获取本年开始时间和结束时间** @param condition* @return*/public static void setYearRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));condition.put("startTime", calendar.getTime());calendar.add(Calendar.YEAR, 1);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** 获取上一年开始时间和结束时间** @param condition* @return*/public static void setLastYearRange(Map<Object, Object> condition) {Calendar calendar = Calendar.getInstance();calendar.set(calendar.get(Calendar.YEAR), 0, calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));calendar.add(Calendar.YEAR, -1);condition.put("startTime", calendar.getTime());calendar.add(Calendar.YEAR, 1);calendar.set(Calendar.SECOND, -1);condition.put("stopTime", calendar.getTime());}/*** ("startTime" -> "Fri Jun 08 00:00:00 CST 2018")String的Date转为String类型的date*/public static String strToformatDate(String date) {String str = "";Date date1 = new Date();try {DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);date1 = df.parse(date);df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");str = df.format(date1);} catch (Exception e) {e.printStackTrace();}return str;}}
2.测试时间
package com.trs.quwei;
/*** 测试时间** @Author quwei* @Date 2019/09/11*/import org.junit.Test;import java.util.HashMap;
import java.util.Map;public class TestDate {@Testpublic void test1() {Map<Object, Object> condition = new HashMap<>();//DateUtil.setDayRange(condition);//今天//DateUtil.setYesterdayRange(condition);//昨天//DateUtil.setWeekRange(condition);//本周// DateUtil.setLastWeekRange(condition);//上周//DateUtil.setMonthRange(condition);//本月//DateUtil.setLastMonthRange(condition);//上月// DateUtil.setLastQuarterRange(condition);//上季度//DateUtil.setQuarterRange(condition); //本季度//DateUtil.setYearRange(condition);//今年//DateUtil.setLastYearRange(condition);//上年DateUtil.setHalfYearRange(condition);//近半年//DateUtil.setUpHalfYearRange(condition);//上半年//DateUtil.setNextHalfYearRange(condition);//下半年String startTime = String.valueOf(DateUtil.strToformatDate(condition.get("startTime").toString()));String stopTime = String.valueOf(DateUtil.strToformatDate(condition.get("stopTime").toString()));System.out.println("开始结束时间:" + startTime + "--->" + stopTime);}
}
本文标签: JAVA Calendar计算 昨天 今天 上周 本周 上个月
版权声明:本文标题:JAVA Calendar计算 昨天,今天,上周,本周,上个月,本月,上个季度,本季度,近半年,上半年,下半年,上一年,今年 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1731102137h1441534.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论