如何将String类的日期转换成Java的LocalDateTime

今天收到一个修改任务,要求按照日、周、月等不同时间维度返回数据库内的聚合数据。

前端请求只是一个命令的作用,并不能提供什么条件,于是我设计了一个接口。可以根据一个

period参数返回不同时间维度下的统计结果:

首先设计一个工具类:

package com.potevio.networkmonitoringdatabasecreatetable.tool;

import java.time.LocalDateTime;

/**
 * @program: random-number
 * @description: 转换成日期
 * @author: Miller.FAN
 * @create: 2019-05-30 14:15
 **/

public class DateString {

    public LocalDateTime toDate(String date) {
        String strDate = date;
        int a = 0;
        int b = 0;
        int c =0;
        int d =0;
        int year = 0;

        int e = 0;
        int f = 0;
        int month = 0;

        int g = 0;
        int h = 0;
        int day = 0;

        int i = 0;
        int j = 0;
        int hour = 0;

        int k = 0;
        int l =0;
        int min = 0;


        char year4 = strDate.charAt(0);
        a = (int) year4 - (int) ('0');
        char year3 = strDate.charAt(1);
        b = (int) year3 - (int) ('0');

        char year2 = strDate.charAt(2);
        c = (int) year2 - (int) ('0');

        char year1 = strDate.charAt(3);
        d = (int) year1 - (int) ('0');

        year = a*1000 + b*100 +c*10 + d;

        char month2 = strDate.charAt(5);
        e = (int) month2 - (int) ('0');

        char month1 = strDate.charAt(6);
        f = (int) month1 - (int) ('0');

        month = e*10 + f;

        char day2 = strDate.charAt(8);
        g = (int) day2 - (int) ('0');
        char day1 = strDate.charAt(9);
        h = (int) day1 - (int) ('0');
        day = g*10 + h;

        char hour2 = strDate.charAt(11);
        i = (int) hour2 - (int) ('0');
        char hour1 = strDate.charAt(12);
        j = (int) hour1 - (int) ('0');
        hour = i*10 + j;

        char min2 = strDate.charAt(14);
        k = (int) min2 - (int) ('0');
        char min1 = strDate.charAt(15);
        l = (int) min1 - (int) ('0');
        min = k*10 + l;

        LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, min, 00);
        return dateTime;
    }

}

然后就可以条用该工具类中的方法将String格式的DATE转换成LocalDateTime类;

服务层代码:

public List getStatisticsDataService(int period) {

        List list = new ArrayList<>();
        int i = 24;

        int j = 7;

        int k = 30;
        //获取当前时间,确定周期,然后按照固定频率切换起始和结束时间
        LocalDateTime dt = LocalDateTime.now().withNano(0);

        //获取1天之前的时间,一天采样24个点
        LocalDateTime dt_minus_days = dt.minusHours(1);

        //获取一周之前的时间
        LocalDateTime dt_minus_weeks = dt.minusDays(1);

        //获取一个月之前的时间
        LocalDateTime dt_minus_months = dt.minusDays(1);



        if(period == 1) {
            //以天为单位返回数据
            while(i != 0) {

                Object score = networkScoreRepository.getSvgScore(dt_minus_days.toString(),dt.toString());
                if(null == score) {
                    score = 0;
                }
                StaticsData staticsData = new StaticsData();
                staticsData.setScore(score);
                staticsData.setTime(dt.getHour());
                System.out.println("staticsData.time : " + staticsData.getTime());
                list.add(staticsData);

                System.out.println("dt_minus_days: " + dt_minus_days + "dt: " + dt);
                dt = dt.minusHours(1);
                dt_minus_days = dt_minus_days.minusHours(1);
                i--;
            }
        }

        else if(period == 2){
            //以周为单位返回数据
            while(j != 0) {
                Object score = networkScoreRepository.getSvgScore(dt_minus_weeks.toString(),dt.toString());
                if(null == score) {
                    score = 0;
                }
                StaticsData staticsData = new StaticsData();
                staticsData.setScore(score);
                if(dt.getDayOfWeek().toString().equals("MONDAY")) {
                    staticsData.setTime(1);
                }
                else if(dt.getDayOfWeek().toString().equals("TUESDAY")) {
                    staticsData.setTime(2);
                }
                else if(dt.getDayOfWeek().toString().equals("WEDNESDAY")) {
                    staticsData.setTime(3);
                }
                else if(dt.getDayOfWeek().toString().equals("THURSDAY")) {
                    staticsData.setTime(4);
                }
                else if(dt.getDayOfWeek().toString().equals("FRIDAY")) {
                    staticsData.setTime(5);
                }
                else if(dt.getDayOfWeek().toString().equals("SATURDAY")) {
                    staticsData.setTime(6);
                }
                else if (dt.getDayOfWeek().toString().equals("SUNDAY")) {
                    staticsData.setTime(7);
                }
                else {
                    staticsData.setTime(0);
                }
                System.out.println("staticsData.time : " + dt.getDayOfWeek().toString());
                System.out.println("staticsData.time : " + staticsData.getTime());
                System.out.println("dt_minus_weeks: " + dt_minus_weeks + "dt: " + dt);
                list.add(staticsData);
                dt = dt.minusDays(1);
                dt_minus_weeks = dt_minus_weeks.minusDays(1);
                j--;
            }
        }

        else if(period == 3) {
            //以月为单位返回数据
            while(k != 0) {
                Object score = networkScoreRepository.getSvgScore(dt_minus_months.toString(),dt.toString());
                if(null == score) {
                    score = 0;
                }
                StaticsData staticsData = new StaticsData();
                staticsData.setScore(score);
                staticsData.setTime(dt.getDayOfMonth());
                System.out.println("staticsData.time : " + staticsData.getTime());
                System.out.println("dt_minus_months: " + dt_minus_months + "dt: " + dt);
                list.add(staticsData);
                dt = dt.minusDays(1);
                dt_minus_months = dt_minus_months.minusDays(1);
                k--;
            }
        }

        else {
            //do nothing
        }

        return list;
    }

 

你可能感兴趣的:(Java编程,Web开发)