本文主要是介绍java 获取周、月、年的第一天以及最后一天的详细代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public class MyCalendar {public static Date getDate(Calendar c) {
return new Date(c.getTimeInMillis());
}
/**
* 得到本周第一天
* @return
*/
public static Calendar getFirstDayOfWeek() {
Calendar monday = Calendar.getInstance();
return getADayOfWeek(monday, Calendar.MONDAY);
}
/**
* 得到本周第一天
* @param day
* @return
*/
public static Calendar getFirstDayOfWeek(Calendar day) {
Calendar monday = (Calendar) day.clone();
return getADayOfWeek(monday, Calendar.MONDAY);
}
/**
* 得到本周最后一天
* @return
*/
public static Calendar getLastDayOfWeek() {
Calendar sunday = Calendar.getInstance();
return getADayOfWeek(sunday, Calendar.SUNDAY);
}
/**
* 得到本周最后一天
* @param day
* @return
*/
public static Calendar getLastDayOfWeek(Calendar day) {
Calendar sunday = (Calendar) day.clone();
return getADayOfWeek(sunday, Calendar.SUNDAY);
}
private static Calendar getADayOfWeek(Calendar day, int dayOfWeek) {
int week = day.get(Calendar.DAY_OF_WEEK);
if (week == dayOfWeek)
return day;
int diffDay = dayOfWeek - week;
if (week == Calendar.SUNDAY) {
diffDay -= 7;
} else if (dayOfWeek == Calendar.SUNDAY) {
diffDay += 7;
}
day.add(Calendar.DATE, diffDay);
return day;
}
/**
* 得到本月的第一天
* @return
*/
public static Calendar getMonthFirstDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMinimum(Calendar.DAY_OF_MONTH));
return calendar;
}
/**
* 得到本月的最后一天
*
* @return
*/
public static Calendar getMonthLastDay() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, calendar
.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar;
}
/**
* 得到本年度第一天
* @return
*/
public static Calendar getYearFristDay(){
Calendar calendar =Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
calendar.clear();
calendar.set(Calendar.YEAR, year);
return calendar;
}
/**
* 得到本年度最后一天
* @return
*/
public static Calendar getYearLastDay(){
Calendar calendar =Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
calendar.clear();
calendar.set(Calendar.YEAR, year);
calendar.roll(Calendar.DAY_OF_YEAR, -1);
return calendar;
}
public static void main(String[] args){
Calendar day=Calendar.getInstance();
int month=day.get(Calendar.DAY_OF_MONTH);
System.out.println("month:"+month);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String dates=format.format(MyCalendar.getDate(MyCalendar.getFirstDayOfWeek()));
String datee=format.format(MyCalendar.getDate(MyCalendar.getLastDayOfWeek()));
String months=format.format(MyCalendar.getDate(MyCalendar.getMonthFirstDay()));
String monthe=format.format(MyCalendar.getDate(MyCalendar.getMonthLastDay()));
String years=format.format(MyCalendar.getDate(MyCalendar.getYearFristDay()));
String yeare=format.format(MyCalendar.getDate(MyCalendar.getYearLastDay()));
System.out.println("weekday :"+dates+"=========== end:"+datee);
System.out.println("months :"+months+"=========== end:"+monthe);
System.out.println("years :"+years+"=========== end:"+yeare);
}
}
这篇关于java 获取周、月、年的第一天以及最后一天的详细代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!