import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
* 获取 当前年、半年、季度、月、日、小时 开始结束时间
*/
private final SimpleDateFormat shortSdf;
private final SimpleDateFormat longHourSdf;
private final SimpleDateFormat longSdf;

public RemindDateUtils(){
this.shortSdf = new SimpleDateFormat("yyyy-MM-dd");
this.longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");
this.longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}

/**
* 获得本周的第一天,周一
*
* @return
*/
public Date getCurrentWeekDayStartTime() {
Calendar c = Calendar.getInstance();
try {
int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
c.add(Calendar.DATE, -weekday);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
}

/**
* 获得本周的最后一天,周日
*
* @return
*/
public Date getCurrentWeekDayEndTime() {
Calendar c = Calendar.getInstance();
try {
int weekday = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, 8 - weekday);
c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"));
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
}

/**
* 获得本天的开始时间,即2012-01-01 00:00:00
*
* @return
*/
public Date getCurrentDayStartTime() {
Date now = new Date();
try {
now = shortSdf.parse(shortSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 获得本天的结束时间,即2012-01-01 23:59:59
*
* @return
*/
public Date getCurrentDayEndTime() {
Date now = new Date();
try {
now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 获得本小时的开始时间,即2012-01-01 23:59:59
*
* @return
*/
public Date getCurrentHourStartTime() {
Date now = new Date();
try {
now = longHourSdf.parse(longHourSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 获得本小时的结束时间,即2012-01-01 23:59:59
*
* @return
*/
public Date getCurrentHourEndTime() {
Date now = new Date();
try {
now = longSdf.parse(longHourSdf.format(now) + ":59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 获得本月的开始时间,即2012-01-01 00:00:00
*
* @return
*/
public Date getCurrentMonthStartTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.DATE, 1);
now = shortSdf.parse(shortSdf.format(c.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 当前月的结束时间,即2012-01-31 23:59:59
*
* @return
*/
public Date getCurrentMonthEndTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.DATE, 1);
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 当前年的开始时间,即2012-01-01 00:00:00
*
* @return
*/
public Date getCurrentYearStartTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.MONTH, 0);
c.set(Calendar.DATE, 1);
now = shortSdf.parse(shortSdf.format(c.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 当前年的结束时间,即2012-12-31 23:59:59
*
* @return
*/
public Date getCurrentYearEndTime() {
Calendar c = Calendar.getInstance();
Date now = null;
try {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 当前季度的开始时间,即2012-01-1 00:00:00
*
* @return
*/
public Date getCurrentQuarterStartTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3)
c.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
c.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
c.set(Calendar.MONTH, 4);
else if (currentMonth >= 10 && currentMonth <= 12)
c.set(Calendar.MONTH, 9);
c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

/**
* 当前季度的结束时间,即2012-03-31 23:59:59
*
* @return
*/
public Date getCurrentQuarterEndTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 2);
c.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH,;
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获取前/后半年的开始时间
* @return
*/
public Date getHalfYearStartTime(){
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 6){
c.set(Calendar.MONTH, 0);
}else if (currentMonth >= 7 && currentMonth <= 12){
c.set(Calendar.MONTH, 6);
}
c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;

}
/**
* 获取前/后半年的结束时间
* @return
*/
public Date getHalfYearEndTime(){
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 6){
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
}else if (currentMonth >= 7 && currentMonth <= 12){
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}

}

/**
* 获取前一天日期 及星期
*/
public class RemindDateUtils {
private void initDate(){
String[] weekDays = {"周日","周一","周二","周三","周四","周五","周六"};
Calendar cal = Calendar.getInstance();
cal.add(Calendar.Date,-1);
int i = cal.get(Calendar.DAY_OF_WEEK)-1;
if(i<0){
i=0;
}
String yesterday = new SimpleDateFormate("yyyy年MM月dd日").format(cal.getTime());
     String w = weekDays[i];
}

/**
* 获取某月的最后一天
* @Title:getLastDayOfMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
public static String getLastDayOfMonth(int year,int month)
{
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR,year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());

return lastDayOfMonth;
}

/*获得30天*/
public static List getformatDD(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date today = new Date();
Calendar theCa = Calendar.getInstance();
List list=new ArrayList();
for(int i=0;i<30;i++){
theCa.setTime(today);
theCa.add(theCa.DATE, -29+i);//最后一个数字30可改,30天的意思
Date start = theCa.getTime();
String startDate = sdf.format(start);//三十天之前日期
list.add(startDate);
}
return list;
}
/*获得12月*/
public static List getformatMM(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Date today = new Date();
Calendar theCa = Calendar.getInstance();
List list=new ArrayList();
for(int i=0;i<12;i++){
theCa.setTime(today);
theCa.add(theCa.MONTH, -11+i);//最后一个数字30可改,30天的意思
Date start = theCa.getTime();
String startDate = sdf.format(start);//三十天之前日期
list.add(startDate);
}
return list;
}
/*获得5年*/
public static List getformatYYYY(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
Date today = new Date();
Calendar theCa = Calendar.getInstance();
List list=new ArrayList();
for(int i=0;i<5;i++){
theCa.setTime(today);
theCa.add(theCa.YEAR, -4+i);//最后一个数字30可改,30天的意思
Date start = theCa.getTime();
String startDate = sdf.format(start);//三十天之前日期
list.add(startDate);
}
return list;
}

}

java获取当前年、半年、季度、月、日、小时 开始结束时间等的更多相关文章

  1. php获取一年中某一周的开始和结束时间

    PHP来获取一年中的每星期的开始日期和结束日期的代码 函数get_week()通过传入参数$year年份,获取当年第一天和最后一天所在的周数,计算第一周的日期,通过循环获取每一周的第一天和最后一天的日 ...

  2. java获取当前时间的年周月季度等的开始结束时间

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...

  3. SQL Server中如何获取当前年,月,日,时,分,秒

    分类: SQL Server  select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) ...

  4. Java获取的一天、本星期、这个月、本季度、一年等 开始和结束时间

    1.代码 package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...

  5. java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串)

    总结一些日期常用的代码,方便以后直接拿 <code> /** * java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串) * @param year * @param m ...

  6. JAVA获取前一个月的第一天和最后一天

    package com.date; import java.text.SimpleDateFormat; import java.util.Calendar; /** * 默认显示前一个月的第一天和最 ...

  7. JAVA使用LocalDate获取当前日期所在季度的开始日期和结束日期

    需要使用jdk1.8及以上 /** * 获取当前日期所在季度的开始日期和结束日期 * 季度一年四季, 第一季度:1月-3月, 第二季度:4月-6月, 第三季度:7月-9月, 第四季度:10月-12月 ...

  8. java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.uti ...

  9. Java获取某年某月的第一天

    Java获取某年某月的第一天 1.设计源码 FisrtDayOfMonth.java: /** * @Title:FisrtDayOfMonth.java * @Package:com.you.fre ...

随机推荐

  1. Pytest权威教程15-运行Nose用例

    目录 运行Nose用例 使用方法 支持的nose风格 不支持的习语/已知问题 返回: Pytest权威教程 运行Nose用例 Pytest基本支持运行Nose框架格式的测试用例. 使用方法 后安装py ...

  2. 部署Django到云服务器(centos+nginx+mysql+uwsgi+python3)【操作篇(2)】

    接上篇操作篇(1):https://blog.csdn.net/jacky_zhuyuanlu/article/details/82880612 (七)创建Django项目 (1)建立文件夹,存放网站 ...

  3. kubernetes 1.14安装部署helm插件

    简单介绍: Helm其实就是一个基于Kubernetes的程序包(资源包)管理器,它将一个应用的相关资源组织成为Charts,并通过Charts管理程序包.再简单点说,可以当做RHEL/CentOS系 ...

  4. mysql 连接服务器报ERROR 1130 ,mysql服务正常运行

    今天办公电脑的mysql罢工了,折腾了半天,记录一下. 错误如下: ERROR 1130 (HY000): Host 'localhost' is not allowed to connect to ...

  5. unity热更新AssetBundle框架设计_框架篇

    目录 第1 章 : AssetBundle框架整体设计 课时1:实战项目开发AB技术问题分析 10:53 课时2:实战项目开发AB技术解决方案 10:24 课时3:AB框架整体设计方案详解 09:45 ...

  6. JAVA基础之访问控制权限

    包:库单元 1.当编写一个Java源代码文件时,此文件通常被称为编译单元(有时也被称为转译单元). 2.每个编译单元都必须有一个后缀名.java,而在编译单元内则可以有一个public类,该类名称必须 ...

  7. Android系统分区

    Android系统开发时,经常会遇到添加或者调整系统分区大小的问题,下面以mstar的一款产品为例进行分析: (1)mount指令可以查看到板子中挂在的分区,主要关注ext4类型分区,例如tvserv ...

  8. Python有堆栈/堆,如何管理内存?

    Python有堆栈/堆,如何管理内存? - 代码日志 https://codeday.me/bug/20171016/86264.html

  9. Tosca 给定义变量,取内容放到变量里

    可以在TOOLS里 buffer viewer里面搜索查自己的变量

  10. TextView: android:ellipsize="marquee" 跑马灯效果无效的问题

    今天练习的时候想实现一个文字的跑马灯效果,本来想自己手动实现的,不过突然想起来android里的TextView属性似乎自带了这个效果,叫: android:ellipsize ,平时都是把它的属性值 ...