/
获得上个月月份
/
public static String getLastMouth(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMM");
Calendar calendar=CalendargetInstance();
//calendarset(2010,5,0);
calendarsetTime(new Date());
//calendaradd(CalendarMONTH, -1);
//取得上一个月时间
calendarset(CalendarMONTH,calendarget(CalendarMONTH)-1);
String lastMonth= sdfformat(calendargetTime());
return lastMonth;
}
最好一天自己百度下
public class DateUtil { / 获取给定时间所在周的第一天(Sunday)的日期和最后一天(Saturday)的日期 @param calendar @return Date数组,[0]为第一天的日期,[1]最后一天的日期 / public Date[] getWeekStartAndEndDate(Calendar calendar) { Date[] dates = new Date[2]; Date firstDateOfWeek, lastDateOfWeek; // 得到当天是这周的第几天 int dayOfWeek = calendarget(CalendarDAY_OF_WEEK); // 减去dayOfWeek,得到第一天的日期,因为Calendar用0-6代表一周七天,所以要减一 calendaradd(CalendarDAY_OF_WEEK, -(dayOfWeek - 1)); firstDateOfWeek = calendargetTime(); // 每周7天,加6,得到最后一天的日子 calendaradd(CalendarDAY_OF_WEEK, 6); lastDateOfWeek = calendargetTime(); dates[0] = firstDateOfWeek; dates[1] = lastDateOfWeek; return dates; } / 获取给定时间所在月的第一天F的日期和最后一天的日期 @param calendar @return Date数组,[0]为第一天的日期,[1]最后一天的日期 / public Date[] getMonthStartAndEndDate(Calendar calendar) { Date[] dates = new Date[2]; Date firstDateOfMonth, lastDateOfMonth; // 得到当天是这月的第几天 int dayOfMonth = calendarget(CalendarDAY_OF_MONTH); // 减去dayOfMonth,得到第一天的日期,因为Calendar用0代表每月的第一天,所以要减一 calendaradd(CalendarDAY_OF_MONTH, -(dayOfMonth - 1)); firstDateOfMonth = calendargetTime(); // calendargetActualMaximum(CalendarDAY_OF_MONTH)得到这个月有几天 calendaradd(CalendarDAY_OF_MONTH, calendar getActualMaximum(CalendarDAY_OF_MONTH) - 1); lastDateOfMonth = calendargetTime(); dates[0] = firstDateOfMonth; dates[1] = lastDateOfMonth; return dates; } public static void main(String[] args) { DateUtil dateUtil = new DateUtil(); Calendar now = CalendargetInstance(); Date[] weekDates = dateUtilgetWeekStartAndEndDate(now); Date[] monthDates = dateUtilgetMonthStartAndEndDate(now); Systemoutprintln("firstDateOfWeek: " + weekDates[0] + " ,lastDateOfWeek: " + weekDates[1]); Systemoutprintln("firstDateOfMonth: " + monthDates[0] + " ,lastDateOfMonth: " + monthDates[1]); }}
c = CalendargetInstance();
cset(CalendarDAY_OF_YEAR, 1);//本年第一天
Systemoutprintln(sdfformat(cgetTime()));
c = CalendargetInstance();
cadd(CalendarYEAR, 1);
cset(CalendarDAY_OF_YEAR, 1);//本年最后一天
cadd(CalendarDAY_OF_YEAR, -1);
Systemoutprintln(sdfformat(cgetTime()));
Calendar calendar = CalendargetInstance();
//得到月初
calendarset(CalendarDAY_OF_MONTH,calendargetActualMinimum(CalendarDAY_OF_MONTH));
Date strDateFrom = calendargetTime();
//得到月末
calendarset(CalendarDAY_OF_MONTH,calendargetActualMaximum(CalendarDAY_OF_MONTH));
Date strDateTo = calendargetTime();
或者
public static String getMonthFirstDay() {
Calendar calendar = CalendargetInstance();
calendarset(CalendarDAY_OF_MONTH, calendar
getActualMinimum(CalendarDAY_OF_MONTH));
return dateFormat("yyyy-MM-dd", calendargetTime());
}
/
得到本月的最后一天
@return
/
public static String getMonthLastDay() {
Calendar calendar = CalendargetInstance();
calendarset(CalendarDAY_OF_MONTH, calendar
getActualMaximum(CalendarDAY_OF_MONTH));
return dateFormat("yyyy-MM-dd", calendargetTime());
}
或者
private int getLastDayOfMonth(int year, int month) {
Calendar cal = CalendargetInstance();
calset(CalendarYEAR, year);
calset(CalendarMONTH, month);
// 某年某月的最后一天
return calgetActualMaximum(CalendarDATE);
}
需要注意的是:资料均来自JAVAEYE 月份是从0开始的,比如说如果输入5的话,实际上显示的是4月份的最后一天,千万不要搞错了哦
比如今天是2009年3月24号,
那本月第一天就是: 今天的年 + 月 + 01
本月的最后一天就是:(本月第一天)加1月减1天
明白了?
日期的加减可以用类Calendar实现
本月的开始时间,就是年朋+01
结束时间,简单一点就是下月1号减1天
可以使用javautilCalendar来取得相关日期信息,下面给个示例供参考:
Java代码:
import javatextParseException;
import javatextSimpleDateFormat;
import javautilCalendar;
public class DateDemo {
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
public static void main(String[] args) throws ParseException {
String date = "2011-04-25 22:28:30";
Systemoutprintln(date);
Systemoutprintln("年份:" + getYear(date));
Systemoutprintln("月份:" + getMonth(date));
Systemoutprintln("日期:" + getDay(date));
Systemoutprintln("月初日期是: " + getMinMonthDate(date));
Systemoutprintln("月末日期是: " + getMaxMonthDate(date));
}
/
获取日期年份
@param date
@return
@throws ParseException
/
public static int getYear(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return calendarget(CalendarYEAR);
}
/
获取日期月份
@param date
@return
@throws ParseException
/
public static int getMonth(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return (calendarget(CalendarMONTH) + 1);
}
/
获取日期号
@param date
@return
@throws ParseException
/
public static int getDay(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
return calendarget(CalendarDAY_OF_MONTH);
}
/
获取月份起始日期
@param date
@return
@throws ParseException
/
public static String getMinMonthDate(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
calendarset(CalendarDAY_OF_MONTH, calendargetActualMinimum(CalendarDAY_OF_MONTH));
return dateFormatformat(calendargetTime());
}
/
获取月份最后日期
@param date
@return
@throws ParseException
/
public static String getMaxMonthDate(String date) throws ParseException{
Calendar calendar = CalendargetInstance();
calendarsetTime(dateFormatparse(date));
calendarset(CalendarDAY_OF_MONTH, calendargetActualMaximum(CalendarDAY_OF_MONTH));
return dateFormatformat(calendargetTime());
}
}
/
获取指定月份的日历信息
@param year
年
@param month
月
@return
/
public static int[] getMonthCalendar(int year, int month) {
Calendar cl = CalendargetInstance();
clset(year, month, 1);
int firstDay = clgetMinimum(CalendarDAY_OF_MONTH);
int lastDay = clgetMaximum(CalendarDAY_OF_MONTH);
int[] day = new int[lastDay];
for (int i = 0; i < lastDay; i++) {
day[i] = i + firstDay;
}
return day;
}
以上就是关于java如何获取指定月的上月最后一天,传入一个月份,获得传入的这个月的上一个月的最后一天,求解全部的内容,包括:java如何获取指定月的上月最后一天,传入一个月份,获得传入的这个月的上一个月的最后一天,求解、java怎么得到某个月的第一天和最后一天的日期,以2012-06为例、java 怎样得到 begintime=”本年第一个月的第一天 endtime=”本年最后一个月的最后一天“等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)