Java如何根据当天日期获取本周日期段(Month + Day)
程序员文章站
2022-04-07 18:29:31
Java如何根据当天日期获取本周日期段1、获取当前日期要想实现这个功能,首先我们得知道如何获取当天日期,这里我用的是java中的Calendar,为了方便后面获取本周日气段,在这里我创建了一个DataTime类,通过getDate(char data_info)方法来获取当天日期。public class DataTime { public static Calendar calendar = Calendar.getInstance(); public static int ge...
Java如何根据当天日期获取本周日期段(Month + Day)
1、获取当前日期
要想实现这个功能,首先我们得知道如何获取当天日期,这里我用的是java中的Calendar,为了方便后面获取本周日气段,在这里我创建了一个DataTime类,通过getDate(char data_info)方法来获取当天日期。
public class DataTime {
public static Calendar calendar = Calendar.getInstance();
public static int getDate(char data_info){
calendar.setTimeZone(TimeZone.getTimeZone("GMT+8:00")) ;
switch (data_info){
case 'Y':
return calendar.get(Calendar.YEAR) ;
case 'M':
return calendar.get(Calendar.MONTH) + 1 ;
case 'w':
return calendar.get(Calendar.DAY_OF_WEEK) - 1 ;
case 'd':
return calendar.get(Calendar.DAY_OF_MONTH) ;
}
return 0 ;
}
}
对以上代码作几点说明:
(1) calendar.get(Calendar.MONTH) + 1的原因
在格里高利历和罗马儒略历中一年中的第一个月是 JANUARY,它为 0;最后一个月取决于一年中的月份数。 简单来说,因为这个值的初始值是0,因此我们要用它来表示正确的月份时就需要加1。
(2) calendar.get(Calendar.DAY_OF_WEEK) - 1的原因
DAY_OF_WEEK获取的是当前日期是一周中的第几天,而一周从周日算起,因此当我们取得这个值之后,为了要正确的获得当前日期是星期几,因此需要减1,然后通过substring方法(左闭右开)截取正确的字符串。
String weekName = "日一二三四五六" ;
String presentWeek = weekName.substring(getDate('w'), getDate('w') + 1) ;
2、获取本周日期段
①获取本周起始日期
public static String getFirstDayOfWeek(int week){
int mPreDay ;
String month = "月" ;
String day = "日" ;
switch (week){
case 0: {
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') - 6) ;
mPreDay = calendar.get(Calendar.DAY_OF_MONTH) ;
if (mPreDay > getDate('d')) {
int mPreMonth = getDate('M') == 1 ? 12 : getDate('M') - 1 ;
return mPreMonth >= 10 ? mPreMonth + month + mPreDay + day : "0" + mPreMonth + month + mPreDay + day ;
}
return getDate('M') >=10 ? getDate('M') + month + mPreDay + day : "0" + getDate('M')+ month + mPreDay + day ;
}
case 1:
return getDate('M') >=10 ? getDate('M') + month + getDate('d') + day : "0" + getDate('M')+ month + getDate('d') + day ;
case 2:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') - 1);
mPreDay = calendar.get(Calendar.DAY_OF_MONTH);
if (mPreDay > getDate('d')) {
int mPreMonth = getDate('M') == 1 ? 12 : getDate('M') - 1;
return mPreMonth >= 10 ? mPreMonth + month + mPreDay + day : "0" + mPreMonth + month + mPreDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mPreDay + day : "0" + getDate('M') + month + mPreDay + day ;
}
case 3:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') - 2) ;
mPreDay = calendar.get(Calendar.DAY_OF_MONTH) ;
if (mPreDay > getDate('d')) {
int mPreMonth = getDate('M') == 1 ? 12 : getDate('M') - 1 ;
return mPreMonth >= 10 ? mPreMonth + month + mPreDay + day : "0" + mPreMonth + mPreDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mPreDay + day : "0" + getDate('M') + month + mPreDay + day ;
}
case 4:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') - 3);
mPreDay = calendar.get(Calendar.DAY_OF_MONTH);
if (mPreDay > getDate('d')) {
int mPreMonth = getDate('M') == 1 ? 12 : getDate('M') - 1;
return mPreMonth >= 10 ? mPreMonth + month + mPreDay + day : "0" + mPreMonth + month + mPreDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mPreDay + day : "0" + getDate('M') + month + mPreDay + day ;
}
case 5:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') - 4);
mPreDay = calendar.get(Calendar.DAY_OF_MONTH);
if (mPreDay > getDate('d')) {
int mPreMonth = getDate('M') == 1 ? 12 : getDate('M') - 1;
return mPreMonth >= 10 ? mPreMonth + month + mPreDay + day : "0" + mPreMonth + month + mPreDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mPreDay + day : "0" + getDate('M') + month + mPreDay + day ;
}
case 6:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') - 5);
mPreDay = calendar.get(Calendar.DAY_OF_MONTH);
if (mPreDay > getDate('d')) {
int mPreMonth = getDate('M') == 1 ? 12 : getDate('M') - 1;
return mPreMonth >= 10 ?
mPreMonth + month + mPreDay + day
: "0" + mPreMonth + month + mPreDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mPreDay + day : "0" + getDate('M') + month + mPreDay + day ;
}
}
return "0000" ;
}
②获取本周末尾日期
public static String getLastDayOfWeek(int week){
int mNextDay , mNextMouth ;
String month = "月" ;
String day = "日" ;
switch (week){
case 0: {
return getDate('M') >=10 ? getDate('M') + month + getDate('d') + day : "0" + getDate('M') + month + getDate('d') + day ;
}
case 1: {
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') + 6);
mNextDay = calendar.get(Calendar.DAY_OF_MONTH);
if (mNextDay < getDate('d')) {
mNextMouth = getDate('M') == 12 ? getDate('M') + 1 : 1;
return mNextMouth >= 10 ?
mNextMouth + month + mNextDay + day : "0" + mNextMouth + month + mNextDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mNextDay + day : "0" + getDate('M') + month + mNextDay + day ;
}
case 2:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') + 5) ;
mNextDay = calendar.get(Calendar.DAY_OF_MONTH) ;
if (mNextDay < getDate('d')) {
mNextMouth = getDate('M') == 12 ? getDate('M') + 1 : 1 ;
return mNextMouth >= 10 ?
mNextMouth + month + mNextDay + day : "0" + mNextMouth + month + mNextDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mNextDay + day : "0" + getDate('M') + month + mNextDay + day ;
}
case 3:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') + 4) ;
mNextDay = calendar.get(Calendar.DAY_OF_MONTH) ;
if (mNextDay < getDate('d')) {
mNextMouth = getDate('M') == 12 ? getDate('M') + 1 : 1 ;
return mNextMouth >= 10 ?
mNextMouth + month + mNextDay + day : "0" + mNextMouth + month + mNextDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mNextDay + day : "0" + getDate('M') + month + mNextDay + day ;
}
case 4:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') + 3) ;
mNextDay = calendar.get(Calendar.DAY_OF_MONTH) ;
if (mNextDay < getDate('d')) {
mNextMouth = getDate('M') == 12 ? getDate('M') + 1 : 1 ;
return mNextMouth >= 10 ?
mNextMouth + month + mNextDay + day : "0" + mNextMouth + month + mNextDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mNextDay + day : "0" + getDate('M') + month + mNextDay + day ;
}
case 5:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') + 2) ;
mNextDay = calendar.get(Calendar.DAY_OF_MONTH) ;
if (mNextDay < getDate('d')) {
mNextMouth = getDate('M') == 12 ? getDate('M') + 1 : 1 ;
return mNextMouth >= 10 ?
mNextMouth + month + mNextDay + day : "0" + mNextMouth + month + mNextDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mNextDay + day : "0" + getDate('M') + month + mNextDay + day ;
}
case 6:{
calendar.set(Calendar.DAY_OF_MONTH, getDate('d') + 1) ;
mNextDay = calendar.get(Calendar.DAY_OF_MONTH) ;
if (mNextDay < getDate('d')) {
mNextMouth = getDate('M') == 12 ? getDate('M') + 1 : 1 ;
return mNextMouth >= 10 ?
mNextMouth + month + mNextDay + day : "0" + mNextMouth + month + mNextDay + day ;
}
return getDate('M') >= 10 ? getDate('M') + month + mNextDay + day : "0" + getDate('M') + month + mNextDay + day ;
}
}
return "0000" ;
}
调用方法
String week_date = DataTime.getFirstDayOfWeek(DataTime.getDate('w')) + " - " + DataTime.getLastDayOfWeek(DataTime.getDate('w')) ;
本文地址:https://blog.csdn.net/weixin_44421949/article/details/110262410