怎么把2007-12-1转换成2007年12月?还有一个问题,如何知道如果知道某年,某月,怎么知道这个月的最后一天?
例如2007年12月,最后一天是31.
2007年2月,最后一天是28.
例如2007年12月,最后一天是31.
2007年2月,最后一天是28.
解决方案 »
- 请问一下ibatis的between查询语句,哪错了啊
- java rt.jar中访问受限的问题
- JAVA编解码问题
- 高手进来帮帮忙啊,一个棘手的滚动条问题!!
- 使用String的方法indexof
- NullPointerException异常
- (高分相送)我想问一下各位哥哥姐姐,有没有毕业设计做有关J2EE方面的,中间件什么的,老师让我毕业设计做这方面的
- [email protected]快来,谢谢你的资料。不要等到晚上了。
- 请教各位有那些web server支持java
- 哪位好兄弟肯给我一个Jbuilder4的序列号?
- 在Lotus中 系统提示 反对使用java.util.Date中的方法
- java的帮助很多,每次查找都需要登陆sun的网站,为什么sun不作成微软的msdn那样方便呢?
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
return sdf.format(date);
}关于后面那个很简单public statci String lastDayOfMonth(int yyyy,int month) {
Date newDate = new Date(yyyy,month,1);
Calendar temp = Calendar.getInstance(TimeZone.getDefault());
temp.setTime(newDate );
temp.add(temp.MONTH, 1);
temp.add(temp.DATE, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
return sdf.format(temp.getTime());
}核心思想 就是把给定的年月 转换成当月的1号,然后加1个月,日期就变成了下月的1好,然后减1天就成了月末日了。记得C#有更加简单的办法。
import java.text.SimpleDateFormat;
import java.util.Date;
class Test
{
public static void main(String [] args)throws Exception
{
SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy年MM月");
Date d=sdf1.parse("2007-12-1");
System.out.println(sdf2.format(d));
}
}
String str = "2007-12-1";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = format.parse(str);
format = new SimpleDateFormat("yyyy年MM月");
System.out.println(format.format(date));
} catch (Exception e) {
e.printStackTrace();
}
Unhandled exception type ParseException at test.TestDate.main(TestDate.java:17)
try {
temp.setTime(new SimpleDateFormat("yyyy/MM/dd").parse("2007/12/1"));
} catch (ParseException ex) {
ex.printStackTrace();
}
temp.add(temp.MONTH, 1);
temp.add(temp.DATE, -1);
System.out.println("output:" + new SimpleDateFormat("yyyy/MM/dd").format(temp.getTime()));
另:new Date(yyyy,month,1); 这样的用法jdk1.5已不推荐使用.
System.out.println(c.getActualMaximum(Calendar.DATE));