public class Study2 {
public static void main(String[] args) {
String setmonth="200803";
int thisYear = Integer.valueOf(setmonth.substring(0,4)).intValue
int thisMonth = Integer.valueOf(setmonth.substring(4,6)).intValue();
try{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdNowDate = new SimpleDateFormat("yyyy-MM-01");
SimpleDateFormat sdNowDate1 = new SimpleDateFormat("yyyy-MM-dd");
cal.set(Calendar.YEAR, thisYear);
cal.set(Calendar.MONTH, thisMonth-1);
String nowTime1 = sdNowDate.format(cal.getTime());
System.out.println("--------nowTime1---------"+nowTime1);//取得3月份第一天
cal.set(Calendar.MONTH, thisMonth);
String nowTime0 = sdNowDate.format(cal.getTime());
System.out.println("--------nowTime0---------"+nowTime0);//取得4月份第一天
Date abc = sdNowDate1.parse(nowTime0);
cal.setTime(abc);
cal.add(Calendar.DATE,-1);
String nowTime2 = sdNowDate1.format(cal.getTime());//取得3月份最后一天
System.out.println("---------nowTime2----------"+nowTime2);
}catch(Exception e){
e.printStackTrace();
}
}
}这是我的程序,主要目的是取得某月的第一天和最后一天。比如String setmonth="200803";那我就要取得3月份第一天和最后一天并以2008-03-01、2008-03-31这样的格式打印出来。现在遇到个奇怪的问题,当我把系统日期改成比如说7月31号,这个方法就有错了主要表现在setmonth="200803"的时候当月最后一天就变成2008-04-30,setmonth="200801",setmonth="200805"
的时候也是这种情况 7月,9月 11月就正常。只有前半年1,3,5月出来的最后一天都是下个月的最后一天。如果系统时间不是月底的话 就没有这种情况 谁知道这是为什么? 或者哪位大侠再给我个好方法,我觉得我这个方法比较笨。取最后一天我是用下个月第一天减去一天实现的。
public static void main(String[] args) {
String setmonth="200803";
int thisYear = Integer.valueOf(setmonth.substring(0,4)).intValue
int thisMonth = Integer.valueOf(setmonth.substring(4,6)).intValue();
try{
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdNowDate = new SimpleDateFormat("yyyy-MM-01");
SimpleDateFormat sdNowDate1 = new SimpleDateFormat("yyyy-MM-dd");
cal.set(Calendar.YEAR, thisYear);
cal.set(Calendar.MONTH, thisMonth-1);
String nowTime1 = sdNowDate.format(cal.getTime());
System.out.println("--------nowTime1---------"+nowTime1);//取得3月份第一天
cal.set(Calendar.MONTH, thisMonth);
String nowTime0 = sdNowDate.format(cal.getTime());
System.out.println("--------nowTime0---------"+nowTime0);//取得4月份第一天
Date abc = sdNowDate1.parse(nowTime0);
cal.setTime(abc);
cal.add(Calendar.DATE,-1);
String nowTime2 = sdNowDate1.format(cal.getTime());//取得3月份最后一天
System.out.println("---------nowTime2----------"+nowTime2);
}catch(Exception e){
e.printStackTrace();
}
}
}这是我的程序,主要目的是取得某月的第一天和最后一天。比如String setmonth="200803";那我就要取得3月份第一天和最后一天并以2008-03-01、2008-03-31这样的格式打印出来。现在遇到个奇怪的问题,当我把系统日期改成比如说7月31号,这个方法就有错了主要表现在setmonth="200803"的时候当月最后一天就变成2008-04-30,setmonth="200801",setmonth="200805"
的时候也是这种情况 7月,9月 11月就正常。只有前半年1,3,5月出来的最后一天都是下个月的最后一天。如果系统时间不是月底的话 就没有这种情况 谁知道这是为什么? 或者哪位大侠再给我个好方法,我觉得我这个方法比较笨。取最后一天我是用下个月第一天减去一天实现的。
楼主【zhang1x1】截止到2008-08-01 09:24:39的历史汇总数据(不包括此帖):
发帖的总数量:4 发帖的总分数:102 每贴平均分数:25
回帖的总数量:5 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:3 结贴的总分数:72
无满意结贴数:0 无满意结贴分:0
未结的帖子数:1 未结的总分数:30
结贴的百分比:75.00 % 结分的百分比:70.59 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
楼主加油
取消马甲机器人,请点这里:http://www.java2000.net/mycsdn/robotStop.jsp?usern=zhang1x1
SimpleDateFormat sformat=new SimpleDateFormat("yyyy-MM-01");
String today=sformat.format(nowTime);
date1=format.parse(today); 得到最后一天示例:Calendar a=Calendar.getInstance();
a.set(Calendar.DATE, 1);//把日期设置为当月第一天
a.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
int MaxDate=a.get(Calendar.DATE);
System.out.println("该月最大天数:"+MaxDate); 说明:没做过测试。
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(){}
public MyDate(int y,int m,int d ){
this.setYear(y);
this.setMonth(m);
this.setDay(d);
}
public void setYear( int y ){
if(y>1900&&y<2100){
year=y;
}else{
System.out.println("year error");
}}
public void setMonth( int m){
if (m>0&&m<13){
month=m;
}else{
System.out.println("month error");
}
}
public void setDay( int d ){
int maxDay=31;
if(year==0||month==0){
return;
}
if(month==4||month==6||month==9||month==11){
maxDay=30;
}else if (month==2){
if(year%4==0&&year%100!=0||year%400==0){
maxDay=29;
}else{
maxDay=28;
}
}
if(d<=maxDay&&d>0){
day=d;
}
else{
System.out.println("day error");
}}
public int getYear(){
return year;}
public int getMonth(){
return month;}
public int getDay(){
return day;}
public static void main(String[] args) {
new MyDate(2008,4,29);
}
}可以看看