天天看点

java获取当前年、半年、季度、月、日、小时 开始结束时间等

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");

}

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();

}

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();

}

public Date getCurrentDayStartTime() {

Date now = new Date();

try {

now = shortSdf.parse(shortSdf.format(now));

} catch (Exception e) {

e.printStackTrace();

}

return now;

}

public Date getCurrentDayEndTime() {

Date now = new Date();

try {

now = longSdf.parse(shortSdf.format(now) + " 23:59:59");

} catch (Exception e) {

e.printStackTrace();

}

return now;

}

public Date getCurrentHourStartTime() {

Date now = new Date();

try {

now = longHourSdf.parse(longHourSdf.format(now));

} catch (Exception e) {

e.printStackTrace();

}

return now;

}

public Date getCurrentHourEndTime() {

Date now = new Date();

try {

now = longSdf.parse(longHourSdf.format(now) + ":59:59");

} catch (Exception e) {

e.printStackTrace();

}

return now;

}

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;

}

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;

}

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;

}

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;

}

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;

}

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, 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;

}

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;

}

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是线程不安全的