java获取最后一天
1. java中获取去年最后一天的日期
直接子串就行,不需要转换两次。
2. 关于java中如何获取当前年份的第一天和最后一天
packagecom.dada.test;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
/**
*@authorli_yueling
*@version1.02011-03-25
*
*/
publicclassDateUtil{
/**
*默认日期格式
*/
publicstaticStringDEFAULT_FORMAT="yyyy-MM-dd";
/**
*测试主方法
*@paramargs
*/
publicstaticvoidmain(String[]args){
for(inti=1951;i<1960;i++){
System.out.println(formatDate(getYearFirst(i)));
System.out.println(formatDate(getYearLast(i)));
}
System.out.println(formatDate(getCurrYearFirst()));
System.out.println(formatDate(getCurrYearLast()));
}
/**
*格式化日期
*@paramdate日期对象
*@returnString日期字符串
*/
publicstaticStringformatDate(Datedate){
SimpleDateFormatf=newSimpleDateFormat(DEFAULT_FORMAT);
StringsDate=f.format(date);
returnsDate;
}
/**
*获取当年的第一天
*@paramyear
*@return
*/
(){
CalendarcurrCal=Calendar.getInstance();
intcurrentYear=currCal.get(Calendar.YEAR);
returngetYearFirst(currentYear);
}
/**
*获取当年的最后一天
*@paramyear
*@return
*/
(){
CalendarcurrCal=Calendar.getInstance();
intcurrentYear=currCal.get(Calendar.YEAR);
returngetYearLast(currentYear);
}
/**
*获取某年第一天日期
*@paramyear年份
*@returnDate
*/
publicstaticDategetYearFirst(intyear){
Calendarcalendar=Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR,year);
DatecurrYearFirst=calendar.getTime();
returncurrYearFirst;
}
/**
*获取某年最后一天日期
*@paramyear年份
*@returnDate
*/
publicstaticDategetYearLast(intyear){
Calendarcalendar=Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR,year);
calendar.roll(Calendar.DAY_OF_YEAR,-1);
DatecurrYearLast=calendar.getTime();
returncurrYearLast;
}
}
3. java 知道月份怎么获取第一天 最后一天
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Test {
/**
* 获取某月的最后一天
* @Title:getLastDayOfMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
public static String getLastDayOfMonth(int year,int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR,year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* 获取这个月的最后一天
* @Title:getLastDayOfCurrentMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
public static String getLastDayOfCurrentMonth() {
Calendar cal = Calendar.getInstance();
//获取某月最大天数
int lastDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* 获取某月的第一天
* @Title:getLastDayOfMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
public static String getFirstDayOfMonth(int year,int month) {
Calendar cal = Calendar.getInstance();
//设置年份
cal.set(Calendar.YEAR,year);
//设置月份
cal.set(Calendar.MONTH, month-1);
//获取某月最大天数
int lastDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* 获取这个月的第一天
* @Title:getFirstDayOfCurrentMonth
* @Description:
* @param:@param year
* @param:@param month
* @param:@return
* @return:String
* @throws
*/
public static String getFirstDayOfCurrentMonth() {
Calendar cal = Calendar.getInstance();
//获取某月最大天数
int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
//格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime());
return lastDayOfMonth;
}
/**
* @Title:main
* @Description:
* @param:@param args
* @return: void
* @throws
*/
public static void main(String[] args)
{
String lastDay = getLastDayOfMonth(2014,2);
System.out.println("获取2014年2月的最后一天:" + lastDay);
String currentLDay= getLastDayOfCurrentMonth();
System.out.println("获取本月的最后一天:" + currentLDay);
String firstDay = getFirstDayOfMonth(2014,2);
System.out.println("获取2014年2月的第一天:" + firstDay);
String currentFDay= getFirstDayOfCurrentMonth();
System.out.println("获取本月的第一天:" + currentFDay);
}
}
传上来的代码真难看,不知道怎么发文件,我给你截一张图
4. java中怎么获取当前年份的第一天和最后一天
Datenow=newDate();
Calendarc=Calendar.getInstance();
c.setTime(now);
c.set(Calendar.DAY_OF_YEAR,1);
System.out.println(c.getTime());//第一天
c.add(Calendar.YEAR,1);
c.set(Calendar.DAY_OF_YEAR,-1);
System.out.println(c.getTime());//最后一天
5. java中如何获取当前月最后一天
publicStringgetLastMonthDay(){Calendarcal=Calendar.getInstance();Datedate=newDate();cal.setTime(date);intyear=0;intmonth=cal.get(Calendar.MONTH);//上个月月份//intday1=cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数intday=cal.getActualMaximum(Calendar.DAY_OF_MONTH);//结束天数System.out.println("###lastmonth:"+month);if(month==0){year=cal.get(Calendar.YEAR)-1;month=12;}else{year=cal.get(Calendar.YEAR);}StringendDay=year+"-"+month+"-"+day;returnendDay+"23:59:59"}
6. java中如何获取当前月的最后一天的日期
getLeastMaximum
public abstract int getLeastMaximum(int field)
返回此 Calendar 实例给定日历字段的最低的最大值。最低的最大值被定义为 getActualMaximum(int)
方法为任何可能时间值返回的最小值。最低的最大值取决于日历系统实例的特定参数。例如,对于格里高利历系统,Calendar 将为
DAY_OF_MONTH 字段返回 28,因为第 28 天是该日历最短那个月的最后一天,即普通年的二月。
7. java获取每个月最后一天的日期
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String args[]) {
System.out.println(getLastDayOfMonth(new Date()));
}
public static Date getLastDayOfMonth(Date sDate1) {
Calendar cDay1 = Calendar.getInstance();
cDay1.setTime(sDate1);
final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
Date lastDate = cDay1.getTime();
lastDate.setDate(lastDay);
return lastDate;
}
}