java之本周、上周、开始、结束时间
前言:
近日在这里跌了好几次,博客写下来、祭奠失去的这几日;参考了几个博客、原地址忘记了,路过留言 、偶会加上
package com.rcplatform.livechat.util;import org.springframework.stereotype.Component;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;/*** 时间相关工具类* Created by phoebeM on 2018/12/29.*/
@Component
public class TimeTool {public String dateToString(Date date){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");return sdf.format(date);}public String getTime(Long time){SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date date = new Date(time);return simpleDateFormat.format(date);}public Date stringToDate(String time){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");try {return sdf.parse(time);} catch (ParseException e) {e.printStackTrace();}return new Date();}public Date getLastWeekMonday() {Calendar cal = Calendar.getInstance();cal.setTime(getCurrentMonday());cal.add(Calendar.DATE, -7);return cal.getTime();}public Date getLastMondayEndDay() {Calendar cal = Calendar.getInstance();cal.setTime(getLastWeekMonday());return getDayEndTime( cal.getTime());}//获取明天的开始时间public Date getBeginDayOfTomorrow() {Calendar cal = new GregorianCalendar();cal.setTime(getCurrentMonday());cal.add(Calendar.DAY_OF_MONTH, 1);return cal.getTime();}public Date getLastMondayStartTime(){return getDayStartTime(getLastWeekMonday());}public Date getMondayStartTime(){return getDayStartTime(getCurrentMonday());}public Date getCurrentMonday() {return getCalendar().getTime() ;}public Date getMondayEndTime(){return getDayEndTime(getCurrentMonday());}public Date getSundayEndTime(){return getDayEndTime(getCurrentSunday());}public Date getCurrentSunday(){Calendar cal =getCalendar();cal.add(Calendar.DATE, 6);return cal.getTime();}// 获取本周的开始时间public Date getBeginDayOfWeek() {Date date = new Date();if (date == null) {return null;}Calendar cal = Calendar.getInstance();cal.setTime(date);int dayofweek = cal.get(Calendar.DAY_OF_WEEK);if (dayofweek == 1) {dayofweek += 7;}cal.add(Calendar.DATE, 2 - dayofweek);return getDayStartTime(cal.getTime());}// 获取本周的结束时间public Date getEndDayOfWeek() {Calendar cal = Calendar.getInstance();cal.setTime(getBeginDayOfWeek());cal.add(Calendar.DAY_OF_WEEK, 6);Date weekEndSta = cal.getTime();return getDayEndTime(weekEndSta);}/*** 获取上周的结束时间* @return*/public Date getLastSundayEndDay() {Calendar cal = Calendar.getInstance();cal.setTime(getLastWeekMonday());cal.add(Calendar.DAY_OF_WEEK, 6);return getDayEndTime(cal.getTime());}public Date getDayEndTime(Date date) {Calendar todayEnd = new GregorianCalendar();todayEnd.setTime(date);todayEnd.set(Calendar.HOUR_OF_DAY, 23);todayEnd.set(Calendar.MINUTE, 59);todayEnd.set(Calendar.SECOND, 59);todayEnd.set(Calendar.MILLISECOND, 999);return todayEnd.getTime();}public Date getDayStartTime(Date date) {Calendar todayStart = new GregorianCalendar();todayStart.setTime(date);todayStart.set(Calendar.HOUR_OF_DAY, 0);todayStart.set(Calendar.MINUTE, 0);todayStart.set(Calendar.SECOND, 0);todayStart.set(Calendar.MILLISECOND, 0);return todayStart.getTime();}private Calendar getCalendar(){Calendar cal = Calendar.getInstance();cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);return cal;}//判断选择的日期是否是本周public boolean isThisWeek(Date time) {Date mondayStartTime = getMondayStartTime();Date sondayEndTime = getSundayEndTime();//当前时间 在上周一之后 上周日之前if(mondayStartTime.getTime() <= time.getTime() && sondayEndTime.getTime() >= time.getTime()){return true;}return false;}}
java之本周、上周、开始、结束时间相关推荐
- Java 输出执行开始时间,结束时间和运行时间
long start,end; start = System.currentTimeMillis(); end = System.currentTimeMillis(); System.out.pri ...
- 获取本周开始和结束时间
/*** 获取本周开始和结束时间** @return String[] 返回格式"yyyy-MM-dd HH:mm:ss* @author xyl* @date 2023/1/4 13:25 ...
- Java获取今天 开始和结束时间
1 /** 2 * 获取今天开始时间 3 */ 4 private Long getStartTime() { 5 Calendar todayStart = Calendar.getInstance ...
- java获取当天开始,结束时间
//获取当天结束时间 public static Date getEndTime(Date date) {Calendar dateEnd = Calendar.getInstance();dateE ...
- java joda_java-Jodatime的开始时间和结束时间
对于那些来这里寻找" js-joda"答案的人,您有两种选择,具体取决于要完成的工作 选项1:您希望同一时区的一天开始 由于您已选择根据与时区相关的即时时间来计算时间,因此应使用Z ...
- java输出执行开始时间,结束时间和运行时间
在程序开发中经常会用到比较代码执行效率问题,可以用System.currentTimeMillis()方法来记录一下 long start,end; start = System.currentTim ...
- Java 日期工具类(今天、昨天、本周、上周、本月、上月、本年及特定时间的开始时间和结束时间)
timestamp格式的部分 /*** 获取当天的开始时间* @return Timestamp* @author LoveEmperor_王子様* date: 2019/8/15 15:50*/pu ...
- 计算当前周、月、季、半年、一年和上周、上月、上季、当前半年的上一个半年、上年的开始和结束时间
下面的例子是一个完整的工具类,记录了各种获取开始时间和结束时间的方法,其中本人用过的个别方法为开始时间和结束时间格式为 2019-10-10 00:00:00 ,2019-10-10 23:59:59 ...
- java取开始时间和结束时间_java获取指定开始时间与结束时间之间的所有日期
本文实例为大家分享了java获取指定开始时间与结束时间之间的所有日期的具体代码,供大家参考,具体内容如下 import java.text.SimpleDateFormat; import java. ...
最新文章
- html会在哪里显示,CSS如何显示 HTML元素
- Python单例模式中几种实现及优化方法
- mobx中跟新了数据视图没变化_【第1781期】MobX 简明教程
- WPF特效-粒子动画
- linux内容对齐的快捷键,linux第一波命令总结
- 录入商品信息的c语言,超市商品信息系统设计报告及程序C语言.doc
- Windows server常见操作、问题
- 剑指offer面试题33. 二叉搜索树的后序遍历序列(二叉树)(递归)
- Windows解除网速限制,Windows性能提升,性能优化
- JP-Word 简谱编辑(JPW简谱)打谱软件免费版下载 WiN
- MySQL利用关系代数进行查询_mysql – 将SQL SELECT查询转换为相应的/等效的关系代数...
- GPS 经纬度转换 百度、高德经纬度
- python 基础面试题(一)
- 微信公众号后台添加安全域名 提示:无法访问xxx指向的web服务器(或虚拟主机)的目录,请检查网络设置
- 自我营销(转帖自 	 TI E2E 工程师社区 (Beta))
- IP 地址冲突检测程序源码(解决某种情况下检测无效的问题)
- Linux easy-rsa制作证书
- 超越美日,世界级项目准备起步,基建狂魔是如何管理隧道
- 吴信东--数据挖掘top10算法
- 达人评测 锐龙r7 5800x3D相当于i几 r75800x3d参数