Java取值过去时间方法sql&JavaDemo
2021/7/30 19:37:34
本文主要是介绍Java取值过去时间方法sql&JavaDemo,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
数据查询: 传递的时间是字符串时间(202107) 需要拿到前两个月的时间(也就是要以202105的时间去查询数据)
两种解决方案:
-
在sql语句层面取到指定的时间来进行业务处理
select to_date('20161230','yyyymmdd')-to_date('20161201','yyyymmdd') from dual;
-
通过Java代码层面, 首先可以写个方法, 拿到上个月的时间, 然后再调用一下, 就可以拿到上上个月的时间;
package com.biostime.merchant.wx; import com.biostime.merchant.wx.exception.MerchantWxException; import org.apache.commons.lang.StringUtils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * @author TongHu * @date 2021/7/30 - 14:09 */ public class TestDate { public static void main(String[] args)throws Exception { String lastYearMonthByYearMonth = getLastYearMonthByYearMonth(null); System.out.println("lastYearMonthByYearMonth = " + lastYearMonthByYearMonth); String lastYearMonthByYearMonth1 = getLastYearMonthByYearMonth(lastYearMonthByYearMonth); System.out.println("lastYearMonthByYearMonth1 = " + lastYearMonthByYearMonth1); } public static String getLastYearMonthByYearMonth(String yearMonth) throws Exception{ SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); Date date = null; try { if (StringUtils.isNotBlank(yearMonth)) { date = format.parse(yearMonth); } else { date = new Date(); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 设置为当前时间 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月 date = calendar.getTime(); String accDate = format.format(date); return accDate; } catch (Exception e) { throw new MerchantWxException("日期格式有误"); } } } package com.biostime.merchant.wx; import com.biostime.merchant.wx.exception.MerchantWxException; import org.apache.commons.lang.StringUtils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * @author TongHu * @date 2021/7/30 - 14:09 */ public class TestDate { public static void main(String[] args)throws Exception { String lastYearMonthByYearMonth = getLastYearMonthByYearMonth(null); System.out.println("lastYearMonthByYearMonth = " + lastYearMonthByYearMonth); String lastYearMonthByYearMonth1 = getLastYearMonthByYearMonth(lastYearMonthByYearMonth); System.out.println("lastYearMonthByYearMonth1 = " + lastYearMonthByYearMonth1); } public static String getLastYearMonthByYearMonth(String yearMonth) throws Exception{ SimpleDateFormat format = new SimpleDateFormat("yyyyMM"); Date date = null; try { if (StringUtils.isNotBlank(yearMonth)) { date = format.parse(yearMonth); } else { date = new Date(); } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); // 设置为当前时间 calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月 date = calendar.getTime(); String accDate = format.format(date); return accDate; } catch (Exception e) { throw new MerchantWxException("日期格式有误"); } } }
这篇关于Java取值过去时间方法sql&JavaDemo的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-20软考高项学习:新手入门指南
- 2024-11-20软考考前冲刺学习:轻松备考指南
- 2024-11-20软考论文讲解学习:新手入门攻略
- 2024-11-20软考论文指导学习:新手入门指南
- 2024-11-20软考培训学习:新手入门全指南
- 2024-11-20软考选择题学习:从入门到掌握的简单教程
- 2024-11-20软考培训入门指南:轻松掌握软考必备技能
- 2024-11-20软考认证入门教程:轻松掌握IT认证考试
- 2024-11-20软考试题解析与备考指南
- 2024-11-20软考选择题解题技巧入门指南