java获取当前时间的年周月季度等的开始结束时间
2017-08-29 09:16
477 查看
参考:http://blog.csdn.net/augus3344/article/details/51015704
项目中常常用到,昵称一个月修改一次,活动一周参加一次等等类似的需求,索性把可能用到的所有方法都写出来,放在一个工具类里面。
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by xiaochun on 2016/3/24.
*/
public class TimeUtil {
public static void main(String[] args) {
System.out.println("当前小时开始:"+getCurrentHourStartTime().toString());
System.out.println("当前小时结束:"+getCurrentHourEndTime().toString());
System.out.println("当前天开始:"+getCurrentDayStartTime().toString());
System.out.println("当前天时结束:"+getCurrentDayEndTime().toString());
System.out.println("当前周开始:"+getCurrentWeekDayStartTime().toString());
System.out.println("当前周结束:"+getCurrentWeekDayEndTime().toString());
System.out.println("当前月开始:"+getCurrentMonthStartTime().toString());
System.out.println("当前月结束:"+getCurrentMonthEndTime().toString());
System.out.println("当前季度开始:"+getCurrentQuarterStartTime().toString());
System.out.println("当前季度结束:"+getCurrentQuarterEndTime().toString());
System.out.println("当前半年/后半年开始:"+getHalfYearStartTime().toString());
System.out.println("当前半年/后半年结束:"+getHalfYearEndTime().toString());
System.out.println("当前年开始:"+getCurrentYearStartTime().toString());
System.out.println("当前年结束:"+getCurrentYearEndTime().toString());
}
/**
* 获取 当前年、半年、季度、月、日、小时 开始结束时间
*/
private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");;
private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");;
/**
* 获得本周的第一天,周一
*
* @return
*/
public static 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 static 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"));
bc11
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
}
/**
* 获得本天的开始时间
*
* @return
*/
public static Date getCurrentDayStartTime() {
Date now = new Date();
try {
now = shortSdf.parse(shortSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本天的结束时间
*
* @return
*/
public static Date getCurrentDayEndTime() {
Date now = new Date();
try {
now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本小时的开始时间
*
* @return
*/
public static Date getCurrentHourStartTime() {
Date now = new Date();
try {
now = longHourSdf.parse(longHourSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本小时的结束时间
*
* @return
*/
public static Date getCurrentHourEndTime() {
Date now = new Date();
try {
now = longSdf.parse(longHourSdf.format(now) + ":59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本月的开始时间
*
* @return
*/
public static 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;
}
/**
* 本月的结束时间
*
* @return
*/
public static 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;
}
/**
* 当前年的开始时间
*
* @return
*/
public static 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;
}
/**
* 当前年的结束时间
*
* @return
*/
public static 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;
}
/**
* 当前季度的开始时间
*
* @return
*/
public static 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;
}
/**
* 当前季度的结束时间
*
* @return
*/
public static 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, 8);
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 static 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 static 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;
}
}
项目中常常用到,昵称一个月修改一次,活动一周参加一次等等类似的需求,索性把可能用到的所有方法都写出来,放在一个工具类里面。
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* Created by xiaochun on 2016/3/24.
*/
public class TimeUtil {
public static void main(String[] args) {
System.out.println("当前小时开始:"+getCurrentHourStartTime().toString());
System.out.println("当前小时结束:"+getCurrentHourEndTime().toString());
System.out.println("当前天开始:"+getCurrentDayStartTime().toString());
System.out.println("当前天时结束:"+getCurrentDayEndTime().toString());
System.out.println("当前周开始:"+getCurrentWeekDayStartTime().toString());
System.out.println("当前周结束:"+getCurrentWeekDayEndTime().toString());
System.out.println("当前月开始:"+getCurrentMonthStartTime().toString());
System.out.println("当前月结束:"+getCurrentMonthEndTime().toString());
System.out.println("当前季度开始:"+getCurrentQuarterStartTime().toString());
System.out.println("当前季度结束:"+getCurrentQuarterEndTime().toString());
System.out.println("当前半年/后半年开始:"+getHalfYearStartTime().toString());
System.out.println("当前半年/后半年结束:"+getHalfYearEndTime().toString());
System.out.println("当前年开始:"+getCurrentYearStartTime().toString());
System.out.println("当前年结束:"+getCurrentYearEndTime().toString());
}
/**
* 获取 当前年、半年、季度、月、日、小时 开始结束时间
*/
private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");;
private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");;
/**
* 获得本周的第一天,周一
*
* @return
*/
public static 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 static 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"));
bc11
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
}
/**
* 获得本天的开始时间
*
* @return
*/
public static Date getCurrentDayStartTime() {
Date now = new Date();
try {
now = shortSdf.parse(shortSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本天的结束时间
*
* @return
*/
public static Date getCurrentDayEndTime() {
Date now = new Date();
try {
now = longSdf.parse(shortSdf.format(now) + " 23:59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本小时的开始时间
*
* @return
*/
public static Date getCurrentHourStartTime() {
Date now = new Date();
try {
now = longHourSdf.parse(longHourSdf.format(now));
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本小时的结束时间
*
* @return
*/
public static Date getCurrentHourEndTime() {
Date now = new Date();
try {
now = longSdf.parse(longHourSdf.format(now) + ":59:59");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
/**
* 获得本月的开始时间
*
* @return
*/
public static 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;
}
/**
* 本月的结束时间
*
* @return
*/
public static 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;
}
/**
* 当前年的开始时间
*
* @return
*/
public static 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;
}
/**
* 当前年的结束时间
*
* @return
*/
public static 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;
}
/**
* 当前季度的开始时间
*
* @return
*/
public static 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;
}
/**
* 当前季度的结束时间
*
* @return
*/
public static 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, 8);
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 static 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 static 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;
}
}
相关文章推荐
- java获取当前时间的年周月季度等的开始结束时间
- java获取当前年、半年、季度、月、日、小时 开始结束时间等
- java 获取当前时间的天、周、月、季度、半年度、年度开始和结束时间
- java获取当前年、半年、季度、月、日、小时 开始结束时间等
- JavaScript获取当前时间字符串、当前日期字符串、周/月/季度/半年度/年度的开始日期和结束日期
- java Calendar 日期获取工具类,昨日开始时间,昨日结束时间,当前周周一日期,当前时间上月第一天,获取当前年第一天
- 获取当前时间的天、周、月、季度、半年度、年度开始和结束时间
- java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月
- java获取本月开始时间和结束时间、上个月第一天和最后一天的时间以及当前日期往前推一周、一个月
- 获取当前时间的天、周、月、季度、半年度、年度开始和结束时间
- Java获取的一天、本星期、这个月、本季度、一年等 开始和结束时间
- C# 获取两个时间段之间的所有时间与获取当前时间所在的季度开始和结束时间
- Java获取当天、本周、本月、本季度、本年等 开始及结束时间
- Java获取当天、本周、本月、本季度、本年等 开始及结束时间
- C# 根据第几周和季度 获取开始时间和结束时间
- java获取指定年月的开始时间与结束时间
- C#获取指定月指定周的日期范围 根据当前时间获取本月开始日期和结束日期
- python 获取当年、季度、月、日的开始和结束时间
- Java获取时间范围: 当前季度,上个季度,昨天,当前月,上个月
- 使用java的Calendar对象获得当前日期的上几个度开始、结束时间