自我救赎之路--Java(数据处理)

对数据的简单处理

最近做了一个项目,把execl中的数据导入到数据,进行分析制造数据,其实大家并不关心是什么样的数据,要什么样的数据,就跟你们一样,我只关心技术点和要实现的功能。Come on,还是来说说技术点吧!

  1. execl数据导入到数据库

  2. 导入到数据库之后对数据进行处理

    • 替换数据库中某个字段的前缀
      update tableName set filed1="replace"(filed1,'NX.','Tag.') where filed2 like'NX.%

    • 把一个表中的字段替换成另一表中的字段 前提是table1和table2 中有一个字段是相等的值 如filed3=filed4
      update table1 set filed1= (select filed2 from table2 where filed3=filed4 )

    • 一个表中把一个具体到时间段的字段值赋给另一个字段,但是时间要求控制到日期就可以了
      update table set filed1= date_trunc('day',filed2)

  3. 数据库中的数据整理好之后,来用编程处理数据

    • 主要是都日期的操作
/**
 * Created by chenwangming on 2017/8/22.
 */
public  class DifferentDate {

    /**
     * date2比date1多的天数
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2)
    {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1= cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if(year1 != year2) //同一年
        {
            int timeDistance = 0 ;
            for(int i = year1 ; i < year2 ; i ++)
            {
                if(i%4==0 && i%100!=0 || i%400==0) //闰年
                {
                    timeDistance += 366;
                }
                else //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2-day1) ;
        }
        else //不同年
        {
            System.out.println("判断day2 - day1 : " + (day2-day1));
            return day2-day1;
        }
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDaysByMillisecond(Date date1,Date date2)
    {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }

    /**
     * 日期加一天
     * @param date
     * @return
     */
    public static Date addonedate(Date date,int day)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, day);// 今天+1天
        Date tomorrow = c.getTime();
        return tomorrow;
    }

    /**
     *  日期加一小时
     * @param date
     * @param time
     * @return
     */
    public static Date addonetime(Date date,int time)
    {
        Calendar ca=Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.HOUR_OF_DAY, time);
        return ca.getTime();
    }

    /**
     * 日期加一天
     * @param date
     * @return
     */
    public static Date addonedateByMillisecond(Date date)
    {
        long time=date.getTime()+24*3600*1000;
        try   {
            Date  d  =  new Date(time);
           return d;
        }
        catch(Exception ex) {
            return new Date();
        }
    }
    //比较日期大小算出秒数
    public static  long getmillisecond(Date date1,Date date2)
    {
        long millisecond=0;
        if(date1.getTime()>date2.getTime())
        {
            millisecond= (date1.getTime()-date2.getTime())/1000;
        }else
        {
            millisecond= (date2.getTime()-date1.getTime())/1000;
        }
        return millisecond;
    }

    // 自造24小时制
    public static List<Date> time24hours(Date time) throws ParseException {
        List<Date> datetime=new ArrayList<Date>();
        //region old
//        for(int i=0;i<24;i++)
//        {
//            String hh="";
//            if(i<10)
//            {
//                hh="0"+i;
//            }else
//            {
//                hh=String.valueOf(i);
//            }
//            String pattan="yyyy-MM-dd "+hh+":00:00";
//            SimpleDateFormat sdf = new SimpleDateFormat(pattan);
//            String dtime=sdf.format(time);
//            ParsePosition pos=new ParsePosition(0);
//            Date timeone= sdf.parse(dtime,pos);
//
//            datetime.add(timeone);
//        }
        //endregion

        String pattan="yyyy-MM-dd 00:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat(pattan);
        ParsePosition pos=new ParsePosition(0);
        Date timeone= sdf.parse(sdf.format(time),pos);

        //region new
        for(int i=0;i<24;i++)
        {
            datetime.add(addonetime(timeone,i));
        }
        //endregion
        return datetime;
    }


    //把开始日期到结束日期放在List<Date>集合中
    public static  List<Date> getListDate(Date start,Date end) throws ParseException {
        String pattan="yyyy-MM-dd 00:00:00";
        SimpleDateFormat sdf = new SimpleDateFormat(pattan);
        Date starttime= sdf.parse(sdf.format(start));
        int days= differentDays(start,end);
        List<Date> listDay=new ArrayList<Date>();
        //listDay.add(starttime);
        for(int i=0;i<=days;i++)
        {
            Date dateadd=addonedate(starttime,i);
            listDay.add(dateadd);
        }
        return listDay;
    }
    //获取数组最小数据的索引
    public static int getMin(long[] arr)
    {
        long mix = arr[0];
        int index=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] < mix){
                mix = arr[i];
            }
        }
        for(int j=0;j<arr.length;j++)
        {
            if(arr[j]==mix)
            {
                index=j;
                break;
            }
        }
        //int index= Arrays.binarySearch(arr,mix); 有问题
        return index;
    }

    //一个日期集合和一个日期对比最, 取出最小的日期
    public static Date getmixcompore(List<Date> datelist,Date date)
    {
            long[] arr=new long[datelist.size()];

            for(int i=0;i<datelist.size();i++)
            {
               long length= getmillisecond(datelist.get(i),date);
               arr[i]=length;
            }
            int index= getMin(arr);

           return datelist.get(index);
    }

对了,以上我用的数据库是postgreSql,这些就是我对数据的处理,其中用到一个取出数组中最小元素的索引,我用了两个for循环, Arrays.binarySearch(arr,mix);这个反法有问题,取的不准确。我知道性能不好,可是,没有想到合适的方法,等待后期的优化。

经历过黑暗,才有对光明的渴望;经历过风雨,才懂得阳光的温暖;经历过沧桑,才拥有温柔的内心;经历人生最好的成长。

共同学习,共同进步,技术交流群:210470210

猜你喜欢

转载自blog.csdn.net/ccwm0129/article/details/77505534