熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

Java 獲取指定日期的實現方法總結

2022-06-13   來源: JSP教程 
以下是對Java中獲取指定日期的實現方法進行了歸納總結需要的朋友可以參考下   復制代碼 代碼如下:
格式化日期 String>Date  或者 Data>String

  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
Date  date = sdfparse("");//String>Date  

  String  sdate  = sdfformat(date );// Data>String ===============================================================
package comhefengtest;
import javatextDateFormat;
import javatextParsePosition;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilDate;
import javautilGregorianCalendar;
public class TimeTest {
//用來全局控制 上一周本周下一周的周數變化
private int weeks = ;
private int MaxDate;//一月最大天數
private int MaxYear;//一年最大天數
/**
* @param args
*/
public static void main(String[] args) {
TimeTest tt = new TimeTest();

  Systemoutprintln("得到個月後的日期:"+tt getAfterMonth();
Systemoutprintln("獲取當天日期:"+ttgetNowTime("yyyyMMdd"));
Systemoutprintln("獲取本周一日期:"+ttgetMondayOFWeek());
Systemoutprintln("獲取本周日的日期~:"+ttgetCurrentWeekday());
Systemoutprintln("獲取上周一日期:"+ttgetPreviousWeekday());
Systemoutprintln("獲取上周日日期:"+ttgetPreviousWeekSunday());
Systemoutprintln("獲取下周一日期:"+ttgetNextMonday());
Systemoutprintln("獲取下周日日期:"+ttgetNextSunday());
Systemoutprintln("獲得相應周的周六:"+ttgetNowTime("yyyyMMdd"));
Systemoutprintln("獲取本月第一天日期:"+ttgetFirstDayOfMonth());
Systemoutprintln("獲取本月最後一天日期:"+ttgetDefaultDay());
Systemoutprintln("獲取上月第一天日期:"+ttgetPreviousMonthFirst());
Systemoutprintln("獲取上月最後一天的日期:"+ttgetPreviousMonthEnd());
Systemoutprintln("獲取下月第一天日期:"+ttgetNextMonthFirst());
Systemoutprintln("獲取下月最後一天日期:"+ttgetNextMonthEnd());
Systemoutprintln("獲取本年的第一天日期:"+ttgetCurrentYearFirst());
Systemoutprintln("獲取本年最後一天日期:"+ttgetCurrentYearEnd());
Systemoutprintln("獲取去年的第一天日期:"+ttgetPreviousYearFirst());
Systemoutprintln("獲取去年的最後一天日期:"+ttgetPreviousYearEnd());
Systemoutprintln("獲取明年第一天日期:"+ttgetNextYearFirst());
Systemoutprintln("獲取明年最後一天日期:"+ttgetNextYearEnd());
Systemoutprintln("獲取本季度第一天到最後一天:"+ttgetThisSeasonTime());
Systemoutprintln("獲取兩個日期之間間隔天數~
:"+TimeTestgetTwoDay(""""));
}

  /**
* 得到指定月後(前)的日期 參數傳負數即可

  */

  public static String  getAfterMonth(int month) {
        Calendar c = CalendargetInstance();//獲得一個日歷的實例  
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");  
        Date date = null;  
        try{  
            date = sdfparse("");//初始日期  
        }catch(Exception e){ 

  }  
        csetTime(date);//設置日歷時間  
        cadd(CalendarMONTHmonth);//在日歷的月份上增加個月  
        String strDate = sdfformat(cgetTime()));//的到你想要得個月後的日期  
        return strDate;

  }

  
/**
* 得到二個日期間的間隔天數
*/
public static String getTwoDay(String sj String sj) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMdd");
long day = ;
try {
javautilDate date = myFormatterparse(sj);
javautilDate mydate = myFormatterparse(sj);
day = (dategetTime() mydategetTime()) / ( * * * );
} catch (Exception e) {
return "";
}
return day + "";
}
/**
* 根據一個日期返回是星期幾的字符串
*/
public static String getWeek(String sdate) {
// 再轉換為時間
Date date = TimeTeststrToDate(sdate);
Calendar c = CalendargetInstance();
csetTime(date);
// int hour=cget(CalendarDAY_OF_WEEK);
// hour 中存的就是星期幾了其范圍 ~
// =星期日 =星期六其他類推
return new SimpleDateFormat("EEEE")format(cgetTime());
}
/**
* 將短時間格式字符串轉換為時間 yyyyMMdd
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
ParsePosition pos = new ParsePosition();
Date strtodate = formatterparse(strDate pos);
return strtodate;
}
/**
* 兩個時間之間的天數
*
* @param date
* @param date
* @return
*/
public static long getDays(String date String date) {
if (date == null || dateequals(""))
return ;
if (date == null || dateequals(""))
return ;
// 轉換為標准時間
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyyMMdd");
javautilDate date = null;
javautilDate mydate = null;
try {
date = myFormatterparse(date);
mydate = myFormatterparse(date);
} catch (Exception e) {
}
long day = (dategetTime() mydategetTime()) / ( * * * );
return day;
}
// 計算當月最後一天返回字符串
public String getDefaultDay(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateset(CalendarDATE);//設為當前月的
lastDateadd(CalendarMONTH);//加一個月變為下月的
lastDateadd(CalendarDATE);//減去一天變為當月最後一天
str=sdfformat(lastDategetTime());
return str;
}
// 上月第一天
public String getPreviousMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateset(CalendarDATE);//設為當前月的
lastDateadd(CalendarMONTH);//減一個月變為下月的
//lastDateadd(CalendarDATE);//減去一天變為當月最後一天
str=sdfformat(lastDategetTime());
return str;
}
//獲取當月第一天
public String getFirstDayOfMonth(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateset(CalendarDATE);//設為當前月的
str=sdfformat(lastDategetTime());
return str;
}
// 獲得本周星期日的日期
public String getCurrentWeekday() {
weeks = ;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus+);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
//獲取當天時間
public String getNowTime(String dateformat){
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat(dateformat);//可
以方便
地修改日期格式
String hehe = dateFormatformat(now);
return hehe;
}
// 獲得當前日期與本周日相差的天數
private int getMondayPlus() {
Calendar cd = CalendargetInstance();
// 獲得今天是一周的第幾天星期日是第一天星期二是第二天
int dayOfWeek = cdget(CalendarDAY_OF_WEEK); //因為按中國禮拜一
作為第一
天所以這裡減
if (dayOfWeek == ) {
return ;
} else {
return dayOfWeek;
}
}
//獲得本周一的日期
public String getMondayOFWeek(){
weeks = ;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
//獲得相應周的周六的日期
public String getSaturday() {
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus + * weeks + );
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
// 獲得上周星期日的日期
public String getPreviousWeekSunday() {
weeks=;
weeks;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus+weeks);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
// 獲得上周星期一的日期
public String getPreviousWeekday() {
weeks;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus + * weeks);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
// 獲得下周星期一的日期
public String getNextMonday() {
weeks++;
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus + );
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
// 獲得下周星期日的日期
public String getNextSunday() {
int mondayPlus = thisgetMondayPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATE mondayPlus + +);
Date monday = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preMonday = dfformat(monday);
return preMonday;
}
private int getMonthPlus(){
Calendar cd = CalendargetInstance();
int monthOfNumber = cdget(CalendarDAY_OF_MONTH);
cdset(CalendarDATE );//把日期設置為當月第一天
cdroll(CalendarDATE );//日期回滾一天也就是最後一天
MaxDate=cdget(CalendarDATE);
if(monthOfNumber == ){
return MaxDate;
}else{
return monthOfNumber;
}
}
//獲得上月最後一天的日期
public String getPreviousMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarMONTH);//減一個月
lastDateset(CalendarDATE );//把日期設置為當月第一天
lastDateroll(CalendarDATE );//日期回滾一天也就是本月最後一天
str=sdfformat(lastDategetTime());
return str;
}
//獲得下個月第一天的日期
public String getNextMonthFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarMONTH);//減一個月
lastDateset(CalendarDATE );//把日期設置為當月第一天
str=sdfformat(lastDategetTime());
return str;
}
//獲得下個月最後一天的日期
public String getNextMonthEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarMONTH);//加一個月
lastDateset(CalendarDATE );//把日期設置為當月第一天
lastDateroll(CalendarDATE );//日期回滾一天也就是本月最後一天
str=sdfformat(lastDategetTime());
return str;
}
//獲得明年最後一天的日期
public String getNextYearEnd(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarYEAR);//加一個年
lastDateset(CalendarDAY_OF_YEAR );
lastDateroll(CalendarDAY_OF_YEAR );
str=sdfformat(lastDategetTime());
return str;
}
//獲得明年第一天的日期
public String getNextYearFirst(){
String str = "";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
Calendar lastDate = CalendargetInstance();
lastDateadd(CalendarYEAR);//加一個年
lastDateset(CalendarDAY_OF_YEAR );
str=sdfformat(lastDategetTime());
return str;
}
//獲得本年有多少天
private int getMaxYear(){
Calendar cd = CalendargetInstance();
cdset(CalendarDAY_OF_YEAR);//把日期設為當年第一天
cdroll(CalendarDAY_OF_YEAR);//把日期回滾一天
int MaxYear = cdget(CalendarDAY_OF_YEAR);
return MaxYear;
}
private int getYearPlus(){
Calendar cd = CalendargetInstance();
int yearOfNumber = cdget(CalendarDAY_OF_YEAR);//獲得當天是一年中的第幾天
cdset(CalendarDAY_OF_YEAR);//把日期設為當年第一天
cdroll(CalendarDAY_OF_YEAR);//把日期回滾一天
int MaxYear = cdget(CalendarDAY_OF_YEAR);
if(yearOfNumber == ){
return MaxYear;
}else{
return yearOfNumber;
}
}
//獲得本年第一天的日期
public String getCurrentYearFirst(){
int yearPlus = thisgetYearPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATEyearPlus);
Date yearDay = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preYearDay = dfformat(yearDay);
return preYearDay;
}
//獲得本年最後一天的日期 *
public String getCurrentYearEnd(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormatformat(date);
return years+"";
}

  //獲得上年第一天的日期 *
public String getPreviousYearFirst(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormatformat(date); int years_value = IntegerparseInt
(years);
years_value;
return years_value+"";
}
//獲得上年最後一天的日期
public String getPreviousYearEnd(){
weeks;
int yearPlus = thisgetYearPlus();
GregorianCalendar currentDate = new GregorianCalendar();
currentDateadd(GregorianCalendarDATEyearPlus+MaxYear*weeks+(MaxYear));
Date yearDay = currentDategetTime();
DateFormat df = DateFormatgetDateInstance();
String preYearDay = dfformat(yearDay);
getThisSeasonTime();
return preYearDay;
}
//獲得本季度
public String getThisSeasonTime(int month){
int array[][] = {{}{}{}{}};
int season = ;
if(month>=&&month<=){
season = ;
}
if(month>=&&month<=){
season = ;
}
if(month>=&&month<=){
season = ;
}
if(month>=&&month<=){
season = ;
}
int start_month = array[season][];
int end_month = array[season][];
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方
便地修
改日期格式
String years = dateFormatformat(date);
int years_value = IntegerparseInt(years);
int start_days =;//years+""+StringvalueOf(start_month)+"
";//getLastDayOfMonth(years_valuestart_month);
int end_days = getLastDayOfMonth(years_valueend_month);
String seasonDate =
years_value+""+start_month+""+start_days+";"+years_value+""+end_month+""+end_days;
return seasonDate;
}
/**
* 獲取某年某月的最後一天
* @param year 年
* @param month 月
* @return 最後一天
*/
private int getLastDayOfMonth(int year int month) {
if (month == || month == || month == || month == || month ==
|| month == || month == ) {
return ;
}
if (month == || month == || month == || month == ) {
return ;
}
if (month == ) {
if (isLeapYear(year)) {
return ;
} else {
return ;
}
}
return ;
}
/**
* 是否閏年
* @param year 年
* @return
*/
public boolean isLeapYear(int year) {
return (year % == && year % != ) || (year % == );
}


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20190.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.