站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题如何得到本周的周一和周七的日期?

如何得到本周的周一和周七的日期?

摘要: 如何得到本周的周一和周七的日期?


如何得到本周的周一和周七的日期?
如何得到上周的周一和周七的日期?
如何得到上月的第一天和最后一天的日期?


据说,java 基础类中有一个叫Calendar 的东东……


能给代码吗?那个我不怎么熟悉


如果你需要 zerobugforsoftware@163.com 联系


获得当天的DATE日期
Calendar.getInstance().get(Calendar.DAY_OF_MONTH)

今天是星期几
Calendar.getInstance().get(Calendar.DAY_OF_WEEK)

然后自己判断 后面没问题了吧~! 接分


如何得到上月的第一天和最后一天的日期?


Calendar


上月最后一天用本月第一天-1天即可

Calendar的时间类型加减功能特别强大, 好好看看


Calendar.getInstance().get(int filed)..自己去设置...

.....接分


传说中的高手......



/**
* Get the date of monday in this week
*
* @return yyyy-MM-dd
*/
public static String getMondayOfThisWeek() {
String strTemp = "";
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 1);
strTemp = c.get(1) + "-";
if (c.get(2) + 1 < 10)
strTemp += "0";
strTemp = strTemp + (c.get(2) + 1) + "-";
if (c.get(5) < 10)
strTemp += "0";
strTemp += c.get(5);
return strTemp;
}

/**
* Get the date of sunday in this week
*
* @return yyyy-MM-dd
*/
public static String getSundayOfThisWeek() {
String strTemp = "";
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 7);
strTemp = c.get(1) + "-";
if (c.get(2) + 1 < 10)
strTemp += "0";
strTemp = strTemp + (c.get(2) + 1) + "-";
if (c.get(5) < 10)
strTemp += "0";
strTemp += c.get(5);
return strTemp;
}


网上好多的,也收藏了很多,都懒的写拉


呵呵,来晚了,楼上的都搞定了


/**
* Get the date of monday in this week
*
* @return yyyy-MM-dd
*/
public static String getMondayOfThisWeek() {
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 1);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(c.getTime());
}

/**
* Get the date of sunday in this week
*
* @return yyyy-MM-dd
*/
public static String getSundayOfThisWeek() {
Calendar c = Calendar.getInstance();
int dayofweek = c.get(Calendar.DAY_OF_WEEK) - 1;
if (dayofweek == 0)
dayofweek = 7;
c.add(Calendar.DATE, -dayofweek + 7);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(c.getTime());
}


Calendar.getInstance().get(Calendar.DAY_OF_MONTH)//get Date
Calendar.getInstance().get(Calendar.DAY_OF_WEEK)//get week



↑返回目录
前一篇: 哎,求助:wrapper can not find class dealer.Mydealer or class it depends on...
后一篇: 类调用问题!