求教Java 获得当前日期的上月时间

jya126 2010-09-20 10:33:10
最近在做一个人资项目,需要结算当前时间的上个月工资。起初想的是用
Date now = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"),获得当前时间再转换成数字再计算,但感觉不精确,转换也比较麻烦,求教高手有没有简单的方法
...全文
3785 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jya126 2012-07-23
  • 打赏
  • 举报
回复
/**
*
* 方法描述:取得当前日期的上月或下月日期 ,amount=-1为上月日期,amount=1为下月日期;创建人:jya
* @param s_DateStr
* @param s_FormatStr
* @return
* @throws Exception
*/
public static String getFrontBackStrDate(String strDate, String format, int amount) throws Exception {
if (null == strDate) {
return null;
}
try {

DateFormat fmt = new SimpleDateFormat(format);
Calendar c = Calendar.getInstance();
c.setTime(fmt.parse(strDate));
c.add(Calendar.MONTH, amount);
return fmt.format(c.getTime());
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
  • 打赏
  • 举报
回复
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
return c.getTime();

ronniegxq 2010-09-21
  • 打赏
  • 举报
回复
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
return c.getTime();
yinxiaoqi 2010-09-21
  • 打赏
  • 举报
回复
学习了哈。
tenor 2010-09-21
  • 打赏
  • 举报
回复
select * from sales where MONTH(ord_date) = DATENAME ( month , getdate() )-1 and year(ord_date)=DATENAME ( year , getdate() )
有点啰嗦,就这意思。
whut_lcy 2010-09-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bao110908 的回复:]
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
return c.getTime();
[/Quote]

然。
xianaofei 2010-09-21
  • 打赏
  • 举报
回复
/**
* 取参数月的前一个月
* month: yyyy-MM或yyyyMM
*/
public static String getPreMonth(String _month) {
String preMonth = "";
if (_month.length() == 6) {
_month = _month.substring(0, 4) + "-" + _month.substring(4);
}
if (_month.length() >= 7) {
if (_month.substring(5, 7).equals("01")) { //1月
preMonth = (Integer.parseInt(_month.substring(0, 4))-1) + "-12";
} else {
int im = Integer.parseInt(_month.substring(5, 7))-1;
if (im < 10) {
preMonth = _month.substring(0, 4) + "-0" + im;
} else {
preMonth = _month.substring(0, 4) + "-" + im;
}
}
}

return preMonth;
}
tenor 2010-09-21
  • 打赏
  • 举报
回复
也可以直接在sql中写呀
wzdsll5211314 2010-09-21
  • 打赏
  • 举报
回复
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
return c.getTime();
dr_lou 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bao110908 的回复:]
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
return c.getTime();
[/Quote]

Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd")
String preMonth = dateFormat.format(c.getTime());

然后扔到你的sql语句去吧
thegodofwar 2010-09-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bao110908 的回复:]
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
return c.getTime();
[/Quote]
++1
  • 打赏
  • 举报
回复
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
return c.getTime();
内容概要:本文系统介绍了JavaScript中DOM(文档对象模型)操作的核心概念与API,涵盖DOM树的基本组成——节点(node)类型及其属性方法,重点讲解了Node接口、NodeList与HTMLCollection接口的区别与使用方式。文章进一步阐述了ParentNode、ChildNode接口提供的子节点管理方法,详细说明了Document节点和Element节点的关键属性与操作方法,如查询、创建、插入、删除节点等。此外,还涉及属性操作、文本节点处理、CSS样式控制以及Mutation Observer API的异步监听机制,全面展示了前端开发中对页面结构与样式的动态操控技术。; 适合人群:具备HTML/CSS基础,正在学习JavaScript或前端开发的初学者及有一定经验的开发者;适合希望深入理解DOM机制、提升动态网页交互能力的技术人员。; 使用场景及目标:①掌握DOM节点的遍历与增删改查操作;②理解NodeList与HTMLCollection的区别及转换技巧;③熟练运用querySelector、createElement等常用API进行页面动态更新;④利用Mutation Observer实现对DOM变化的高效监听与响应。; 阅读建议:建议结合实际项目练习文中提到的各类DOM操作方法,重点关注节点关系、属性操作与事件联动,通过调试工具观察DOM树变化,加深对异步监听机制的理解。

67,542

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧