java中如何从日期类型中获取月

java中如何从日期类型中获取月,第1张

//将yyyyMMdd转为date

public static Date getCoreToEmsDateStr(String dateStr){

DateFormat format = new SimpleDateFormat("yyyyMMdd");

Date d = null;

try{

d = formatparse(dateStr);

}catch(ParseException e){

eprintStackTrace();

}

return d;

}

public static String getDateAfterDays(Timestamp s,int days){

Timestamp currTimestamp = s;

for (int i=0;i<days;i++){

currTimestamp = getNextDate(currTimestamp);

}

return getDateTimeStr(currTimestamp,"3");

}

public static Timestamp getNextDate(javasqlTimestamp tsDate){

if(tsDate==null)

return null;

javautilCalendar calendar = CalendargetInstance();

calendarsetTime(tsDate);

return getDateTime(calendarget(CalendarYEAR),calendarget(CalendarMONTH)+1,calendarget(CalendarDATE)+1,

calendarget(CalendarHOUR_OF_DAY),calendarget(CalendarMINUTE),calendarget(CalendarSECOND));

}

public static javasqlTimestamp getDateTime(int year,int month,int day,int hour,int minute,int second){

javasqlTimestamp ts = null;

javautilDate dt = null;

javautilCalendar calendar = CalendargetInstance();

calendarclear();

calendarset(year,month-1,day,hour,minute,second);

dt = calendargetTime();

ts = new javasqlTimestamp(dtgetTime());

return ts;

}

/

比较两个时间是否相同

@param tsBeginDate

@param tsEndDate

@param bool

@return

/

public static long getDateInterval(Timestamp tsBeginDate,Timestamp tsEndDate,boolean bool){

long lDays = 0;

if(bool){

tsBeginDate = CommongetDateTime(CommongetDateString(tsBeginDate),bool);

}

if(tsBeginDate!=null&&tsEndDate!=null){

Log4jinfo("tsEndDategetTime ()===="+tsEndDate);

Log4jinfo("tsBeginDategetTime ()===="+tsBeginDate);

lDays = (tsEndDategetTime()-tsBeginDategetTime())/86400000+1;

Log4jinfo("lDays===="+lDays);

}

return lDays;

}

/

格式化成Timestamp类型

@param sDt

@param bool

@return

/

public static javasqlTimestamp getDateTime(String sDt,boolean bool){

try{

return javasqlTimestampvalueOf(sDt); //sDt format:yyyy-mm-dd hh:mm:ssfffffffff

}catch(IllegalArgumentException iae){

if(bool)

sDt = sDt+" 23:59:590";

else

sDt = sDt+" 00:00:000";

return javasqlTimestampvalueOf(sDt);

}

}

/

根据时间获取日期字符串

@param ts

@return

/

public static String getDateString(Timestamp ts){

if(ts==null)

return "";

Calendar calendar = CalendargetInstance();

calendarsetTime(ts);

String strMonth = StringvalueOf(calendarget(CalendarMONTH)+1);

if(strMonthlength()==1){

strMonth = "0"+strMonth;

}

String strDay = StringvalueOf(calendarget(CalendarDATE));

if(strDaylength()==1){

strDay = "0"+strDay;

}

return calendarget(CalendarYEAR)+"-"+strMonth+"-"+strDay;

}

注意:注释部分是你出错的部分:#include <stdioh>

#include <stdlibh>//仅仅是输出,没有必要int

void nextdate(int y,int m,int d)

{

int flag = 1; //此处增加一个标识,日期是否错误 switch(m)

{

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

//此处建议用大括号表示一个整体,看得清楚;同时日期格式错误

//直接输出错误信息即可,不必计算

{

if(d<31)d++;

else if(d==31)

{

if(m!=12)

{

m++;d=1;

}

else

{

y++;m=1;d=1;

}

}

else

{

printf ("The date is wrong !!\n");flag=0;

}

break;

}

case4:

case6:

case9:

case11:

if(d<30){m++;d=1;}

else

{

printf ("The date is wrong !!\n"); flag=0;

}

break;

case 2:

{

//闰年平年只有在2月份计算,没有必要把f提前计算,放在此处为妥。

int f=((y%4==0)&&(y%100!=0))||(y%400==0); if(!f)

{

if(d<28)d++;

else if(d==28){m++;d=1;}

else

{

printf ("The date is wrong !!\n"); flag=0;

}

break;

}

//是闰年的情况为什么你没有判断呢?我补上了

else

{

if(d<29)d++;

else if(d==29){m++;d=1;}

else

{

printf ("The date is wrong !!\n"); flag=0;

}

break;

}

}

} //最后根据日期格式是否错误决定是否输出明天的日期

if(flag)

{

printf("The date of next day is %d,%d,%d",y,m,d);

}

}

int main()

{

int y,m,d;

printf("input newdate;");

scanf("%d,%d%,%d",&y,&m,&d); //此处scanf格式化错误,应该也用逗号分割,和后面输入的一致

if(y<1820||y>2060)

{

printf ("The date is wrong !!\n");

}

else //此处少了,如果满足条件,则调用函数进行计算明天

{

nextdate(y,m,d);

}

system("pause"); //建议使用pause进行停屏。

}

1 输入是否合法

day_list[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

// 判断条件:

( year>=1920 && year<=2050 ) &&

(

( month>=1 && month<=12 && day>=1 && day<=day_list[month-1] )||

( month==2 && isLeap(year) && day>=1 && day<=29 )

)

2

/

函数说明: int NextDate(int month,int day,int year)

如果还有明天,返回1;

如果输入不合法,返回-1;

如果超出处理范围,返回-2

/

int bLeapYear;

int newY,newM,newD;

newY = year;

newM = month;

newD = day;

bLeapYear = ( (year%4)==0 && (year%100)!=0 ) || (year%400)==0;

if ( year>=1920 && year<=2050 )

{

if ( month>=1 && month<=12 && day>=1 )

{

if ( day<day_list[month-1] )

{

newD += 1;

}

else if ( day==day_list[month-1] && !bLeapYear )

{

if ( month==12 && day==31 )

{

if ( year==2050 ) return -2;

else

{

newY += 1; newM = 1; newD = 1;

}

}

else

{

newM += 1; newD = 1;

}

}

else if ( month==2 && bLeapYear )

{

if ( day==28 ) newD = 29;

else if( day==29 )

{ newM = 3; newD = 1; }

else return -1;

} else return -1;

}

}

// 输出

return 1;

3 测试方法:

31 闰年二月的最后一天

32 闰年二月的第28天

33 非闰年二月的第28天

34 每年的非二月的月末

35 每年的十二月的月末

36 2050年十二月的月末

37

year>2050 || year<1920 || month<1 || month>12 || day<1 ||

(day>day_list[month-1] && month>=1 && month<=12 && month!=2 ) ||

(day>28 && month==2 && !bLeapYear ) ||

(day>29 && month==2 && bLeapYear )

38 闰年非二月的月末 (34的分支)

测试出程序错误:

41

else if ( day==day_list[month-1] && !bLeapYear )

应改为:

else if ( day==day_list[month-1] && month!=2 )

42

else if ( month==2 && bLeapYear )

{

if ( day==28 ) newD = 29;

else if( day==29 )

{ newM = 3; newD = 1; }

else return -1;

}

应改为:

else if ( month==2 )

{

if ( day==28 && bLeapYear ) newD = 29;

else if( day==28 && !bLeapYear || ( day==29 && bLeapYear ) )

{ newM = 3; newD = 1; }

else return -1;

}

/

日历类 作者:lyj

@author Administrator

/

public class MyCalender {

private int year,month,day;//年月日

private static final int BIG_MONTH=31,SMALL_MONTH=30,LEAPYEAR=29,PINGYEAR=28;

private static final int ONE=1,TWELEVE=12;

/

构造函数

@param year 年

@param month 月

@param day 日

/

public MyCalender(int year,int month,int day){

thisyear=year;

thismonth=month;

thisday=day;

}

/

判断日历是否合法

@param my 日历

@return 1 合法 0不合法

/

public int isValid(MyCalender myCal){

//年份不能小于1

//月份不能大于12或者小于1

//日期不能大于31或者小于1

if((myCalyear<ONE) ||(myCalmonth>TWELEVE||myCalmonth<ONE)||

myCalday<ONE||myCalday>BIG_MONTH)

return 0;

return 1;

};

/

返回日历的前一天

@param d

@return

@throws Exception

/

public MyCalender previousDate(MyCalender myDay) throws Exception{

if(isValid(myDay)==0||day>howDay()){

throw new Exception("日历不合法!");

}

else if(day-1==0){

myDaysetMonth(month-1);//月份往前减一

myDaysetDay(howDay());

if(month==0){

myDaysetYear(year-1);

myDaysetMonth(12);

myDaysetDay(howDay());

}

}

else myDaysetDay(day-1);

return myDay;

};

/

返回日历的后一天

@param d

@return

@throws Exception

/

public MyCalender nextDate(MyCalender myDay) throws Exception{

if(isValid(myDay)==0||day>howDay()){

throw new Exception("日历不合法!");

}

//如果后一天大于当前日期的当月天数那么月份+1

else if(day+1>howDay()){

myDaysetDay(1);//日期为1

myDaysetMonth(month+1);

//如果月份大于12的话就年份加1

if(month>12){

myDaysetMonth(1);//月份设置为1

myDaysetYear(year+1);

}

}

else

myDaysetDay(day+1);

return myDay;

};

/

获得当前月份有多少天

@param year

@return

/

private int howDay(){

switch (month) {

case 1:case 3:case 5:case 7 :case 8:case 10:case 12:

return BIG_MONTH;

case 2:

if(isLeapyear())

return LEAPYEAR;

return PINGYEAR;

}

return SMALL_MONTH;

}

/

判断是否是平年

@return

/

protected boolean isLeapyear(){

return ((year%4==0 && year%100!=0)||(year%400==0));

}

@Override

public String toString() {

return year+"年"+month+"月"+day+"日";

};

/set/

public void setYear(int year) {

thisyear = year;

}

public void setDay(int day) {

thisday = day;

}

public void setMonth(int month) {

thismonth = month;

}

/set

//测试入口

@throws Exception /

public static void main(String[] args) throws Exception {

MyCalender my=new MyCalender(2012,11,29);

Systemoutprintln(myhowDay());

Systemoutprintln(mypreviousDate(my));

Systemoutprintln(mynextDate(my));

Systemoutprintln(mynextDate(my));

Systemoutprintln(mynextDate(my));

Systemoutprintln(mynextDate(my));

}

}

楼主你好 目前只实现了你需要的东西

还有就是我新创了一个群 不介意的话来交流 261957883

class MyDate{

public int year,month,day;

public MyDate(MyDate date){

year=dateyear;

month=datemonth;

day=dateday;

}

public boolean isLeapYear(){

return (((year%4==0)&&(year%100!=0))||(year%400==0));

}

public MyDate(int y,int m,int d){

year=y;

month=m;

day=d;

if(((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))&&((day<1)||(day>31))) Systemoutprintln("wrong date");

if(((month==4)||(month==6)||(month==9)||(month==11))&&((day<1)||(day>30))) Systemoutprintln("wrong date");

if(month==2){

if(isLeapYear()&&((day<1)||(day>29))) Systemoutprintln("wrong date");

if(!isLeapYear()&&((day<1)||(day>28))) Systemoutprintln("wrong date");

}

}

public boolean equal(MyDate date){

return (year==dateyear && month==datemonth && day==dateday);

}

public void incrementDay(){

if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)){

int flag=0;

if(day==31){month+=1;flag=1;}

if((day<=30)&&(day>=1)){day+=1;}

if(flag==1) day=1;

flag=0;

}

if((month==4)||(month==6)||(month==9)||(month==11)){

int flag=0;

if(day==30){month+=1;flag=1;}

if((day<=29)&&(day>=1)){day+=1;}

if(flag==1) day=1;

flag=0;

}

if(month==12){

int flag=0;

if(day==31){year+=1;month=1;flag=1;}

if((day>=1)&&(day<=30)){day+=1;}

if(flag==1) day=1;

flag=0;

}

if(month==2){

int flag=0;

if(isLeapYear()){

if(day==29){month+=1;flag=1;}

if((day<=28)&&(day>=1)){day+=1;}

if(flag==1) day=1;

flag=0;

}

else{

if(day==28){month+=1;flag=1;}

if((day<=27)&&(day>=1)){day+=1;}

if(flag==1) day=1;

flag=0;

}

}

}

public int getYear(){

return year;

}

public int getMonth(){

return month;

}

public int getDay(){

return day;

}

public String toString(){

return (year+"-"+month+"-"+day);

}

}

public class Mytest{

public static void main(String[] test){

MyDate date1=new MyDate(2000,2,28);

MyDate date2=new MyDate(2006,12,31);

MyDate date3=new MyDate(date2);

Systemoutprintln(date3equal(date2));

date1incrementDay();

Systemoutprintln(date1getYear()+" "+date1getMonth()+" "+date1getDay());

Systemoutprintln(date1toString());

date2incrementDay();

Systemoutprintln(date2getYear()+" "+date2getMonth()+" "+date2getDay());

Systemoutprintln(date2toString());

}

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/12181055.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存