java获取当前年、半年、季度、月、日、小时 开始结束时间等
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获取当前年、半年、季度、月、日、小时 开始结束时间等的更多相关文章
- php获取一年中某一周的开始和结束时间
PHP来获取一年中的每星期的开始日期和结束日期的代码 函数get_week()通过传入参数$year年份,获取当年第一天和最后一天所在的周数,计算第一周的日期,通过循环获取每一周的第一天和最后一天的日 ...
- java获取当前时间的年周月季度等的开始结束时间
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created b ...
- SQL Server中如何获取当前年,月,日,时,分,秒
分类: SQL Server select GETDATE() as '当前日期',DateName(year,GetDate()) as '年',DateName(month,GetDate()) ...
- Java获取的一天、本星期、这个月、本季度、一年等 开始和结束时间
1.代码 package com.zhaochao.utils; import java.text.SimpleDateFormat; import java.util.Calendar; impor ...
- java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串)
总结一些日期常用的代码,方便以后直接拿 <code> /** * java 获取 获取某年某月 所有日期(yyyy-mm-dd格式字符串) * @param year * @param m ...
- JAVA获取前一个月的第一天和最后一天
package com.date; import java.text.SimpleDateFormat; import java.util.Calendar; /** * 默认显示前一个月的第一天和最 ...
- JAVA使用LocalDate获取当前日期所在季度的开始日期和结束日期
需要使用jdk1.8及以上 /** * 获取当前日期所在季度的开始日期和结束日期 * 季度一年四季, 第一季度:1月-3月, 第二季度:4月-6月, 第三季度:7月-9月, 第四季度:10月-12月 ...
- java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.uti ...
- Java获取某年某月的第一天
Java获取某年某月的第一天 1.设计源码 FisrtDayOfMonth.java: /** * @Title:FisrtDayOfMonth.java * @Package:com.you.fre ...
随机推荐
- Windows下Apache配置多域名项目
<VirtualHost www.a.com:80> DocumentRoot "C:\object\a\web" ServerName http://www.a.co ...
- BFC、IFC、FFC、GFC
FC(Formatting Context) 它是W3C CSS2.1规范中的一个概念,定义的是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用. ...
- Prometheus Consul实现自动服务发现
Prometheus Consul实现自动服务发现 1.概述 Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件. Consul 由 HashiCorp公司用Go语言开发 ...
- c和s标签
<s:if test="#request.iflag=='fj'"> <a title="复检录入" href="javascrip ...
- JavaScript 如何工作的: 事件循环和异步编程的崛起 + 5 个关于如何使用 async/await 编写更好的技巧
原文地址:How JavaScript works: Event loop and the rise of Async programming + 5 ways to better coding wi ...
- 第06组 Alpha冲刺(2/6)
队名:拾光组 组长博客链接 作业博客链接 团队项目情况 燃尽图(组内共享) 组长:宋奕 过去两天完成了哪些任务 主要完成了上传照片模块的信息采集 具体完成了采集用户上传的照片信息.分析图像数据.比对数 ...
- fdconnection自动重连
fdconnection自动重连 1)设置 FDConnection1.ResourceOptions.AutoReconnect := True; 控制自动连接的恢复. 使用AutoReconnec ...
- nginx只允许域名访问,禁止ip访问 禁止其他域名访问
背景:为什么要禁止ip访问页面呢?这样做是为了避免其他人把未备案的域名解析到自己的服务器IP,而导致服务器被断网,我们可以通过禁止使用ip访问的方法,防止此类事情的发生. 解决方法:这里介绍修改配置文 ...
- env (arcpy)
addOutputsToMap (读写) 设置是否应将工具产生的输出数据集添加至应用程序显示. Boolean autoCommit (读写) 支持“自动提交”环境的工具将在 ArcSDE 事务中进行 ...
- 判断一个数组的长度用 Length 还是 SizeOf ?
最近发现一些代码, 甚至有一些专家代码, 在遍历数组时所用的数组长度竟然是 SizeOf(arr); 这不合适! 如果是一维数组.且元素大小是一个字节, 这样用看不出错误, 譬如: var arr ...