返回两个时间段内的季度

参考文章:https://www.cnblogs.com/caoyingjielxq/p/9426972.html

public static Date parse(String string, String  patern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(patern);
        try {
            Date parse = simpleDateFormat.parse(string);
            return parse;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
     
/** * 返回两个时间段内,季度的字符串 * @param minDate * @param maxDate * @return */
     public static List<String> getMonthBetweenDates(String minDate, String maxDate){
        ArrayList<String> result = new ArrayList<String>();
        //HashSet<String> strings = new HashSet<>();
        HashMap<String, Integer> map = new HashMap<>();

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();
        min.setTime(parse(minDate,"yyyy-MM-dd"));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
        max.setTime(parse(maxDate,"yyyy-MM-dd"));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        int i = 1;
        while (curr.before(max)) {
            //result.add(format(curr.getTime(),"yyyy-MM-dd"));
            String season1 = getSeason1(curr.getTime());
            map.put(season1, i);
            curr.add(Calendar.MONTH, 1);
            i++;
        }
        ArrayList<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        for (Map.Entry<String, Integer> entry : entries){
            result.add(entry.getKey());
        }
        return result;
    }

    /**
     *
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
     *
     * @param date
     * @return
     */
    public static String getSeason1(Date date) {
        int season = 0;
        String seasonString = "";
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        int year = c.get(Calendar.YEAR);
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.FEBRUARY:
            case Calendar.MARCH:
                season = 1;
                seasonString = year + "年第一季度";
                break;
            case Calendar.APRIL:
            case Calendar.MAY:
            case Calendar.JUNE:
                season = 2;
                seasonString = year + "年第二季度";
                break;
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.SEPTEMBER:
                season = 3;
                seasonString = year + "年第三季度";
                break;
            case Calendar.OCTOBER:
            case Calendar.NOVEMBER:
            case Calendar.DECEMBER:
                season = 4;
                seasonString = year + "年第四季度";
                break;
            default:
                break;
        }
        return seasonString;
    }

------------恢复内容结束------------

原文地址:https://www.cnblogs.com/prader6/p/11872801.html

时间: 2024-08-29 15:22:12

返回两个时间段内的季度的相关文章

Java获取两个时间段内的所有日期

import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * 获取两个时间段内的所有日期,日期可跨年 */ public class GetBetweenDate { public static void main(String[] args) { List<String>

统计查询时间段内工作日之和

今天有个需求是这样的,要查出本月的工作日之和,在网上搜索了一下,发现看起来最简单的是这样的: 1 DECLARE @DAY DATE,@COUNT INT 2 SET @DAY=CONVERT(VARCHAR(10),Dateadd(dd, -Datepart(dd,GetDate())+ 1,GetDate()), 23) 3 SET @COUNT=0 4 WHILE @DAY<=CONVERT(VARCHAR(100), GetDate(),23) 5 BEGIN 6 SET @COUNT=

如何获取两个任意时间段内的所有日期(及其他处理日期时间的方法总结)

(1)用一下方法获取两个任意时间段内的所有日期,代码如下: #1.将字符串转换成datetime类型 def strtodatetime(datestr,format): return datetime.datetime.strptime(datestr,format) #2.时间转换成字符串,格式为2008-08-02 def datetostr(date): return str(date)[0:10] #3.两个日期相隔多少天,例:2008-10-03和2008-10-01是相隔两天 de

iOS 断当前时间是否在一天的某个时间段内。

应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟这俩时间作对比就好了. 下面提供两条思路: 法1.用NSDate生成当前时间,然后转为字符串,从字符串中取出当前的年.月.日,然后再拼上时.分.秒,然后再将拼接后的字符串转为NSDate,最后用当前的时间跟自己生成的俩NSDate的时间点比较.(该方法比较笨,也不难,但看起来有点太菜了,看上去不怎么

ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。

应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟这俩时间作对比就好了. 下面提供两条思路: 法1.用NSDate生成当前时间,然后转为字符串,从字符串中取出当前的年.月.日,然后再拼上时.分.秒,然后再将拼接后的字符串转为NSDate,最后用当前的时间跟自己生成的俩NSDate的时间点比较.(该方法比较笨,也不难,但看起来有点太菜了,看上去不怎么

mysql查询特定时间段内的数据

SET FOREIGN_KEY_CHECKS=0; -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS t_user; CREATE TABLE t_user ( userId bigint(20) NOT NULL, fullName varchar(64) NOT NULL, userType varchar(16) NOT NULL, addedTime datetime NO

sql语句判断两个时间段是否有交集

场景:  数据库有有两个字段.开始时间<startTime>,和结束时间<endTime>,指定一个时间段(a,b),a表示开始时间,b表示结束时间.看数据库中有没有与(a,b)冲突的时间段,有的话就返回那条记录. 解析:两个时间段相当于两个集合,不过是有顺序的集合.两个时间段有交集细分有四种情况.用sql直接判断无交集的语句可能也有,但是目前没有想到,只想到有交集的语句,如果返回不为空则表明有交集,否则没有交集. select * from test_table where (s

sql语句判断两个时间段是否有交集 &lt;非原创&gt;

场景:  数据库有有两个字段.开始时间<startTime>,和结束时间<endTime>,指定一个时间段(a,b),a表示开始时间,b表示结束时间.看数据库中有没有与(a,b)冲突的时间段,有的话就返回那条记录. 解析:两个时间段相当于两个集合,不过是有顺序的集合.两个时间段有交集细分有四种情况.用sql直接判断无交集的语句可能也有,但是目前没有想到,只想到有交集的语句,如果返回不为空则表明有交集,否则没有交集. view plaincopy select * from test

用redis实现动态时间段内统计排序

问题描述 需要根据某类数据在动态时间段内的统计值对这些数据进行排名.例如按过去24小时内点赞数排名的帖子,每隔一小时计算一次结果.以下描述均针对这个例子展开. 解决思路 针对这种问题,我的第一反应是直接通过 mysql一张数据表记录所有数据的每一条统计值改变的行为,例如记下每个帖子在哪个时间点被谁点赞.排序结果直接通过 select + where + order_by + limit.简单粗暴,但效率低下,扩展性差,而且当数据量很多时,会导致数据库查询效率低下. 那么为了提高效率, mysql