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. SVN优于CVS之处

    1.原子提交.一次提交不管是单个还是多个文件,都是作为一个整体提交的.在这当中发生的意外例如传输中断,不会引起数据库的不完整和数据损坏. 2.重命名.复制.删除文件等动作都保存在版本历史记录当中. 3 ...

  2. python中string、json、bytes的转换

    json->string str = json.dumps(jsonobj) bytes->string str = str(bytes,‘utf-8’) string->json ...

  3. 三大框架 之 SSH整合

    目录 SSH整合 Spring整合web项目 在Servlet当中直接加载配置文件,获取对象 存在问题 解决方案 SSH整合 引入Jar包 引入配置文件 创建包结构 创建相关类 创建Customer业 ...

  4. 小福bbs-冲刺日志(第一天)

    [小福bbs-冲刺日志(第一天)] 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 小福bbs 这个作业的目标 开会,安排具体工作 作业的正文 小福bbs-冲刺日志(第一 ...

  5. Scikit-Learn 机器学习笔记 -- 线性回归、逻辑回归、softma

      import numpy as np from matplotlib import pyplot as plt # 创建线性回归数据集 def create_dataset(): X = 2 * ...

  6. JVM 初始化阶段例子 final常量

    1.创建FinalTest类,里面有一个final常量x class FinalTest{ public static final int x = 3; static { System.out.pri ...

  7. springIoC注解

    一.通过注解注入Bean @Component:组件(spring扫描包下有该注解的类) @ComponentScan(包名):组件扫描(spring扫描该包名下的类) @Configuration: ...

  8. SpringMVC RequestLoggingFilter log to file

    spring - How to Log HttpRequest and HttpResponse in a file? - Stack Overflowhttps://stackoverflow.co ...

  9. vs2017在前端页面使用F12无法转到js脚本函数定义

        这样设置后就可以正常使用了

  10. 在windows上编译chrome浏览器Building Chromium for Windows

    web端用webRTC实现的一对一视频,互动直播和会议.https://github.com/starrtc/android-demo Chromium requires Visual Studio ...