获取日期处理JAVA工具类
程序员文章站
2022-05-17 21:25:30
...
package javatest;
/********************************************
* 模块名称: 工作三年经验,目标是成为JAVA高级开发攻城狮/Java 架构师我在努力!我能行!
* 功能说明: 俩年迷惘期,回首时才发现自己想要的目标和清晰的职业规划
* 开发人员: 马琳-君子自强,脚踏实地积累
* 开发时间:2019/6/27 17:37 今年一定要成为Java高级开发攻城狮
* v1.0.0.0 2019/6/27-01 [java] [email protected]
* 初始版本基础夯实积攒Java架构师的基础内容 test
*******************************************/
import javax.print.DocFlavor;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
/**
* 得到某年某周的第一天
*
* @param year
* @param week
* @return
*/
public static Date getFirstDayOfWeek(int year, int week) {
week = week - 1;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 1);
Calendar cal = (Calendar) calendar.clone();
cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime());
}
/**
* 得到某年某周的最后一天
*
* @param year
* @param week
* @return
*/
public static Date getLastDayOfWeek(int year, int week) {
week = week - 1;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, Calendar.JANUARY);
calendar.set(Calendar.DATE, 1);
Calendar cal = (Calendar) calendar.clone();
cal.add(Calendar.DATE, week * 7);
return getLastDayOfWeek(cal.getTime());
}
/**
* 取得当前日期所在周的第一天
*
* @param date
* @return
*/
public static Date getFirstDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK,
calendar.getFirstDayOfWeek()); // Sunday
return calendar.getTime();
}
/**
* 取得当前日期所在周的最后一天
*
* @param date
* @return
*/
public static Date getLastDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_WEEK,
calendar.getFirstDayOfWeek() + 6); // Saturday
return calendar.getTime();
}
/**
* 取得当前日期所在周的前一周最后一天
*
* @param date
* @return
*/
public static Date getLastDayOfLastWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getLastDayOfWeek(calendar.get(Calendar.YEAR),
calendar.get(Calendar.WEEK_OF_YEAR) - 1);
}
/**
* 返回指定日期的月的第一天
*
* @param year
* @param month
* @return
*/
public static Date getFirstDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH), 1);
return calendar.getTime();
}
/**
* 返回指定年月的月的第一天
*
* @param year
* @param month
* @return
*/
public static Date getFirstDayOfMonth(Integer year, Integer month) {
Calendar calendar = Calendar.getInstance();
if (year == null) {
year = calendar.get(Calendar.YEAR);
}
if (month == null) {
month = calendar.get(Calendar.MONTH);
}
calendar.set(year, month, 1);
return calendar.getTime();
}
/**
* 返回指定日期的月的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH), 1);
calendar.roll(Calendar.DATE, -1);
return calendar.getTime();
}
/**
* 返回指定年月的月的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfMonth(Integer year, Integer month) {
Calendar calendar = Calendar.getInstance();
if (year == null) {
year = calendar.get(Calendar.YEAR);
}
if (month == null) {
month = calendar.get(Calendar.MONTH);
}
calendar.set(year, month, 1);
calendar.roll(Calendar.DATE, -1);
return calendar.getTime();
}
/**
* 返回指定日期的上个月的最后一天
*
* @param year
* @param month
* @return
*/
public static Date getLastDayOfLastMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH) - 1, 1);
calendar.roll(Calendar.DATE, -1);
return calendar.getTime();
}
/**
* 返回指定日期的季的第一天
*
* @param year
* @param quarter
* @return
*/
public static Date getFirstDayOfQuarter(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getFirstDayOfQuarter(calendar.get(Calendar.YEAR),
getQuarterOfYear(date));
}
/**
* 返回指定年季的季的第一天
*
* @param year
* @param quarter
* @return
*/
public static Date getFirstDayOfQuarter(Integer year, Integer quarter) {
Calendar calendar = Calendar.getInstance();
Integer month = new Integer(0);
if (quarter == 1) {
month = 1 - 1;
} else if (quarter == 2) {
month = 4 - 1;
} else if (quarter == 3) {
month = 7 - 1;
} else if (quarter == 4) {
month = 10 - 1;
} else {
month = calendar.get(Calendar.MONTH);
}
return getFirstDayOfMonth(year, month);
}
/**
* 返回指定日期的季的最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfQuarter(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getLastDayOfQuarter(calendar.get(Calendar.YEAR),
getQuarterOfYear(date));
}
/**
* 返回指定年季的季的最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfQuarter(Integer year, Integer quarter) {
Calendar calendar = Calendar.getInstance();
Integer month = new Integer(0);
if (quarter == 1) {
month = 3 - 1;
} else if (quarter == 2) {
month = 6 - 1;
} else if (quarter == 3) {
month = 9 - 1;
} else if (quarter == 4) {
month = 12 - 1;
} else {
month = calendar.get(Calendar.MONTH);
}
return getLastDayOfMonth(year, month);
}
/**
* 返回指定日期的上一季的最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfLastQuarter(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return getLastDayOfLastQuarter(calendar.get(Calendar.YEAR),
getQuarterOfYear(date));
}
/**
* 返回指定年季的上一季的最后一天
*
* @param year
* @param quarter
* @return
*/
public static Date getLastDayOfLastQuarter(Integer year, Integer quarter) {
Calendar calendar = Calendar.getInstance();
Integer month = new Integer(0);
if (quarter == 1) {
month = 12 - 1;
} else if (quarter == 2) {
month = 3 - 1;
} else if (quarter == 3) {
month = 6 - 1;
} else if (quarter == 4) {
month = 9 - 1;
} else {
month = calendar.get(Calendar.MONTH);
}
return getLastDayOfMonth(year, month);
}
/**
* 返回指定日期的季度
*
* @param date
* @return
*/
public static int getQuarterOfYear(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.MONTH) / 3 + 1;
}
public static void main(String args[]) {
Date date =new Date();
SimpleDateFormat F2 =new SimpleDateFormat("yy/MM/dd");
String strDate =F2.format(date);
try {
date=F2.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
Date fdate= getFirstDayOfQuarter(date);
System.out.print(fdate);
}
}
上一篇: Python定时器
下一篇: Python 定时器