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 ...
随机推荐
- (9)打鸡儿教你Vue.js
样式绑定 设置元素的样式 用 v-bind 来设置样式属性 class 与 style 是 HTML 元素的属性 <div v-bind:class="{ active: isActi ...
- Pytest权威教程24-Pytest导入机制及系统路径
目录 Pytest导入机制和sys.path/PYTHONPATH 包中的测试脚本及conftest.py文件 独立测试模块及conftest.py文件 调用通过python -m pytest调用p ...
- ubuntu 14.04 系统配置磁盘,CPU,内存,硬盘信息查看
Linux查看物理CPU个数.核数.逻辑CPU个数# 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 查看分区磁盘 ...
- eclipse快捷键及设置【转】
1.Eclipse设置新建菜单初始项 windows-->Perspective-->Customize Perspective--> 2.Eclipse快捷键 1. ctrl+sh ...
- snprintf用错了快10年…
int snprintf(char *str, size_t size, const char *format, ...); 从用snprintf开始,size参数一直传的都是buff_size-1, ...
- #C++初学记录#日常学习函数
静态变量static,只进行一次初始化. #include<cstring> #include<iostream> using namespace std; int main( ...
- IdHTTPServer开发https服务器
IdHTTPServer开发https服务器 该篇经验同样适用于DATASNAP和UNIGUI,因为它们都基于INDY10. 1)需要TIdServerIOHandlerSSLOpenSSL控件 2) ...
- revenue
美 ['revənju] 英 ['revənjuː] n.收益:营业额:税务署 网络收入:税收:岁入
- 【vue】常用操作
一.Vue中import from的来源:省略后缀与加载文件夹 https://blog.csdn.net/fyyyr/article/details/83657828 二.Vue安装依赖 #安装依赖 ...
- AdapterViewFlipper
使用AdapterViewFiller组件设置图片轮播,该组件可以自动切换view <?xml version="1.0" encoding="utf-8" ...