Created
July 3, 2012 07:18
-
-
Save songpp/3038237 to your computer and use it in GitHub Desktop.
QuarterUtils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.text.ParseException; | |
| import java.util.Calendar; | |
| import java.util.Date; | |
| import java.util.Locale; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| import org.apache.commons.lang.time.DateUtils; | |
| import org.springframework.util.Assert; | |
| /** | |
| * @author songpengpeng | |
| */ | |
| public abstract class QuarterUtils { | |
| private static final Pattern yyyyQn = Pattern.compile("(\\d\\d\\d\\d)[Qq]([1234])"); | |
| public static Date getFirstDateOfQuarter(String term){ | |
| Matcher matcher = yyyyQn.matcher(term); | |
| if(! matcher.matches()) | |
| throw new IllegalArgumentException(term + " not matches yyyyQn"); | |
| Integer y =Integer.parseInt( matcher.group(1)); | |
| Integer q =Integer.parseInt( matcher.group(2)); | |
| int firstMonthOfQ = (q - 1) * 3; | |
| Calendar instance = Calendar.getInstance(Locale.CHINA); | |
| instance.clear(); | |
| instance.set(y, firstMonthOfQ, 1, 0, 0); | |
| return instance.getTime(); | |
| } | |
| /** | |
| * 计算这个日期是第几季度 | |
| * | |
| * e.g. 2011年11月 返回 '2011Q4' | |
| * | |
| * @param date | |
| * @return | |
| */ | |
| public static String getQuarterPrefixed4DigitsYearAndQ(Date date) { | |
| Calendar calendar = Calendar.getInstance(); | |
| calendar.setTime(date); | |
| int year = calendar.get(Calendar.YEAR); | |
| return String.format("%4dQ%1d", year, getQuarter(date)); | |
| } | |
| /** | |
| * 返回`date所在季度的最后一天的零点。 | |
| * @param date | |
| * @return | |
| */ | |
| public static Date getLastDayOfQuarter(final Date date) { | |
| final Date firstDayOfNextQuarter = getFirstDayOfNextQuarter(date); | |
| return DateUtils.addDays(firstDayOfNextQuarter, -1); | |
| } | |
| /** | |
| * 获得下个季度的第一天 00:00:00 | |
| * @param date | |
| * @return | |
| */ | |
| public static Date getFirstDayOfNextQuarter(Date date) { | |
| Date firstDayOfQ = getFirstDayOfQuarter(date); | |
| return DateUtils.addMonths(firstDayOfQ, 3); | |
| } | |
| /** | |
| * 获得当前时间所在季度的最初一天的00:00:00 | |
| * @return Date | |
| */ | |
| public static Date getFirstDayOfCurrentQ() { | |
| return getFirstDayOfQuarter(new Date()); | |
| } | |
| /** | |
| * 获取这个日期所在季度的最初的时间 | |
| * | |
| * 例如 2011-11-24 返回 2011-10-01 00:00:00 | |
| * @param date | |
| * @return Date | |
| */ | |
| public static Date getFirstDayOfQuarter(Date date) { | |
| Calendar instance = Calendar.getInstance(Locale.CHINA); | |
| instance.setTime(date); | |
| int Q = getQuarter(date); | |
| int year = instance.get(Calendar.YEAR); | |
| /** | |
| * JAVA中calendar的月以0为基数。 | |
| */ | |
| int firstMonthOfQ = (Q - 1) * 3; | |
| instance.set(Calendar.YEAR, year); | |
| instance.set(Calendar.MONTH, firstMonthOfQ); | |
| instance.set(Calendar.DAY_OF_MONTH, 1); | |
| instance.set(Calendar.HOUR_OF_DAY, 0); | |
| instance.set(Calendar.MINUTE, 0); | |
| instance.set(Calendar.SECOND, 0); | |
| instance.set(Calendar.MILLISECOND, 0); | |
| return instance.getTime(); | |
| } | |
| /** | |
| * 计算这个日期是第几季度 | |
| * @param date | |
| * @return | |
| */ | |
| public static int getQuarter(Date date) { | |
| Calendar calendar = Calendar.getInstance(Locale.CHINA); | |
| calendar.setTime(date); | |
| int month = calendar.get(Calendar.MONTH); | |
| int q = month / 3 + 1; | |
| return q; | |
| } | |
| public static int getCurrentQuarter() { | |
| return getQuarter(new Date()); | |
| } | |
| public static String getCurrentQuarterPrefixed4DidgitsYear() { | |
| return getQuarterPrefixed4DigitsYearAndQ(new Date()); | |
| } | |
| /** | |
| * Test | |
| * @param args | |
| * @throws ParseException | |
| */ | |
| public static void main(String[] args) throws ParseException { | |
| Date q3 = DateUtils.parseDate("20110911", new String[] { "yyyyMMdd" }); | |
| Assert.isTrue("2011Q3".equals(getQuarterPrefixed4DigitsYearAndQ(q3))); | |
| Date q2 = DateUtils.parseDate("20110511", new String[] { "yyyyMMdd" }); | |
| Assert.isTrue("2011Q2".equals(getQuarterPrefixed4DigitsYearAndQ(q2))); | |
| Date firstDayOfQ3 = QuarterUtils.getFirstDayOfQuarter(q3); | |
| System.out.println(firstDayOfQ3); | |
| Date lastDayOfQ3 = QuarterUtils.getLastDayOfQuarter(q3); | |
| System.out.println(lastDayOfQ3); | |
| Date startOfNextQ = QuarterUtils.getFirstDayOfNextQuarter(new Date()); | |
| System.out.println(startOfNextQ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment