ps:代码有参考别人的,非全部原创。已在正式项目上使用
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];
}
}
PS:SimpleDateFormat是线程不安全的
分享到:
相关推荐
根据输入月份获取季度区间,属于日期时间的工具类!
c#获取当前年的天数,程序完全可以运行,方便参考和学习
java代码获取当前月第一天和最后一天的毫秒值(csdn)————程序
【java】java版本的万年历,可以显示当前时间,如当前年、当前月、当前日、当前小时、当前分钟 说明:java版本的万年历,可以显示当前时间,如当前年、当前月、当前日、当前小时、当前分钟等等
网站统计在线人数,当前年、月、日访问量JAVA代码
js 获取 本周、上周、本月、上月、本季度、上季度的开始结束日期 /** * 获取本周、本季度、本月、上月的开始日期、结束日期 */ var now = new Date(); //当前日期 var nowDayOfWeek = now.getDay(); //今天本周的...
获取出生年月日工具类
C#中获取时间的年月日的方法
"获取某年的第几周的结束日期","获取当前时间所在周的开始日期","当前时间所在周的结束日期","获得上周一的日期","获得本周一的日期","下周一的日期","今天日期","本月一日的日期","获得本月最后一日的日期
非常完美Java实现年、月、日、周访问量统计、
纯JAVA计算日期的农历节日、公历节日代码!我费了很多体力整理了公历和农历的节日。。。要5分就当体力分吧。。。不多吧。。。100多公历节日和几十个农历节日。。。一点点敲出来的。。。还包括代码的实现。
网站统计在线人数,当前年、月、日
该程序是我写的博客“一起talk C栗子吧(第三十七回:C语言实例--获取当前日期和时间)”的配套程序,共享给大家使用
获取当前年的周数 代码如下: /// /// 获得今年有几周 /// /// ”year”> /// <returns></returns> public int GetWeekOfYear(int year) { DateTime the_Date = new DateTime(year,1,1);//本年的第一天 ...
js代码-js 获取当前年-月-日 时:分秒毫秒
C# 常用日期时间函数--DateTime 数字型 ...1.1 取当前年月日时分秒 currentTime=System.DateTim--DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒
2、闰年判定:其次要判断闰年来确定2月的天数,方法为能被4整除且不被100整除或者能被400整除是闰年。这部分写在isLeapYear()函数里。 3、日期的存储:通过开一个二维数组date[12][42]来进行储存。第一个下标代表12...
日期函数在程序开发中是一个重要的工具,js也提供了Date()函数,如获取当前时间,当前年月,已经系统时间的毫秒数。