使用java的Calendar对象获得当前日期的上几个度开始、结束时间
思路:
先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期
/**
* @param flag true:开始日期;false:结束日期
* @return
*/
public static String getLastQuarterTime(boolean flag){
SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String resultDate="";
Date now = null;
try {
Calendar calendar = Calendar.getInstance();
int currentMonth = calendar.get(Calendar.MONTH) + 1;
//true:开始日期;false:结束日期
if(flag){
if (currentMonth >= 1 && currentMonth <= 3)
calendar.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
calendar.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
calendar.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
calendar.set(Calendar.MONTH, 9);
calendar.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(calendar.getTime()) + " 00:00:00");
}else{
if (currentMonth >= 1 && currentMonth <= 3) {
calendar.set(Calendar.MONTH, 2);
calendar.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
calendar.set(Calendar.MONTH, 5);
calendar.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
calendar.set(Calendar.MONTH, 8);
calendar.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
calendar.set(Calendar.MONTH, 11);
calendar.set(Calendar.DATE, 31);
}
now = longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
}
calendar.setTime(now);// 设置日期
calendar.add(Calendar.MONTH, -3);
resultDate = longSdf.format(calendar.getTime()); } catch (Exception e) {
;
}
return resultDate;
}
使用java的Calendar对象获得当前日期的上几个度开始、结束时间的更多相关文章
- Java获取上周,本周,本月,本年,开始结束时间 。日期工具类
由于获取日期经常会使用到,所有我自己写了一个工具类 1.基本上能用上的都写出来了,包括:1)获取当天的开始时间 2)获取当天的结束时间 3)获取昨天的开始时间 4)获取昨天的结束时间 5)获取明天的开 ...
- Java API —— Calendar类
1.Calendar类概述 Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR.MONTH.DAY_OF_MONTH.HOUR 等 日历字段之间的转换提供了一些方法,并为操作日历字段 ...
- Java 中Calendar、Date、SimpleDateFormat学习总结
在之前的项目中,经常会遇到Calendar,Date的一些操作时间的类,并且总会遇到时间日期之间的格式转化问题,虽然做完了但是总是忘记,记不清楚,每次还都要查找资料.今天总结一下,加深印象. Cale ...
- 使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间。
1.使用java的Calendar工具类获取到本月的第一天起始时间和最后一天结束时间. package com.fline.aic.utils; import java.text.DateFormat ...
- Java基础-Calendar类常用方法介绍
Java基础-Calendar类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Calendar类概念 Calendar 类是一个抽象类,它为特定瞬间与一组诸如 Y ...
- Calendar 对象的使用实例
1.Calendar demo例子 JavaCalendar 类时间操作,示范代码. public class CalendarDemo { private static SimpleDateForm ...
- Java学习--Calendar 类的应用
Calendar 类的应用 Date 类最主要的作用就是获得当前时间,同时这个类里面也具有设置时间以及一些其他的功能,但是由于本身设计的问题,这些方法却遭到众多批评,不建议使用,更推荐使用 Calen ...
- Java的Calendar类
通过Date类我们可以创建并格式化一个日期对象,但是如何才能设置和获取日期数据的特定部分呢?----Calendar类 Calendar类是一个抽象类,在实际使用时实现特定的子类的对象,通过getIn ...
- 巧用Java中Calendar工具类
Java的JDK中提供了一系列好用的util工具类.Calendar就是java.util中用于处理日期的工具类.且该工具类易学易用实用. 工具类Calendar是抽象类. PS:为什么把Calend ...
随机推荐
- (转载)Linux系统调用及用户编程接口(API)
(转载)http://www.farsight.com.cn/news/emb167.htm 1 Linux系统调用 所谓系统调用是指操作系统提供给用户程序调用的一组“特殊”接口,用户程序可以通过这组 ...
- Linux中如何新建用户
对于一般用户来说,主目录(home directory)是硬盘上唯一可以原来写东西的地方.一般的路径名是/home/login_user_name. 主目录用于存储各种用户文件:设置文件,程序配置文件 ...
- 南京Uber优步司机奖励政策(2月1日~2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Power of Cryptography
//只用一行核心代码就可以过的天坑题目............= = 题目: Description Current work in cryptography involves (among othe ...
- HDU4283:You Are the One(区间DP)
Problem Description The TV shows such as You Are the One has been very popular. In order to meet the ...
- 仿写自己的一个加载语言包的L函数
<?php /** * [L 加载语言的L的方法] * @param [string] $key [语言键的名称] * @return [string] $value [取到的语言值] */ f ...
- 七周七语言——Prolog(二)
1 递归 首先来看一个知识库: father(zeb,john_boy_sr). father(john_boy_sr,john_boy_jr). ancestor(X,Y):-father(X,Y ...
- [C#] 常用工具类——系统日志类
using System; using System.Collections.Generic; using System.Text; using System.Diagnostics; namespa ...
- Android 获取WIFI MAC地址的方法
1. 经常用法,调用Android的API:WifiManager <uses-permission android:name="android.permission.ACCESS_W ...
- 站在巨人的肩膀上学习Android开发
我们知道,一開始最好的学习方法是模仿,尤其是模仿巨人. 那说到Android开发的模仿自然就是分析并研究主流程序的布局.实现方法.进而提升自己的技术. 第一招----逆向project 要分析&quo ...