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;
}
}