Java中拼接两个时间

package com.atsys.server.commentary.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created  2017/10/10.
 */
public class Test1 {

    public static void main(String[] args) {

        String beginTime = "2017-08-01 19:34:17";
        String endTime = "2018-09-28 12:34:17";
        String classBeginTime = "10:00:00";
        String classEndTime ="10:45:00";

        SimpleDateFormat b = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat cb = new SimpleDateFormat("hh:mm:ss");
        SimpleDateFormat ce = new SimpleDateFormat("hh:mm:ss");

        Date parseBeginTime = null;
        try {
            parseBeginTime = b.parse(beginTime);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        Date parseEndTime = null;
        try {
            parseEndTime = e.parse(endTime);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        Date parseClassBeginTime = null;
        try {
            parseClassBeginTime = cb.parse(classBeginTime);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        Date parseClassEndTime = null;
        try {
            parseClassEndTime = ce.parse(classEndTime);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parseBeginTime);

        //把日期的时分秒设置成0 列如 2017-08-01 00:00:00
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        calendar.set(Calendar.MILLISECOND,0);

        Calendar calendarB = Calendar.getInstance();
        //拼接时间  8时(h)=28800000毫秒(ms)
        calendarB.setTime(new Date(calendar.getTime().getTime()+parseClassBeginTime.getTime()+28800000));
        Calendar calendarE = Calendar.getInstance();
        calendarE.setTime(new Date(calendar.getTime().getTime()+parseClassEndTime.getTime()+28800000));

        while (calendarB.getTime().compareTo(parseEndTime) <= 0){
            System.out.println(b.format(calendarB.getTime())+" ~ "+b.format(calendarE.getTime()));
            //天数进行加一
            calendarB.add(Calendar.DAY_OF_YEAR,1);
            calendarE.add(Calendar.DAY_OF_YEAR,1);
        }
        
    }
}

运行结果:
2017-08-01 10:00:00 ~ 2017-08-01 10:45:00
2017-08-02 10:00:00 ~ 2017-08-02 10:45:00
2017-08-03 10:00:00 ~ 2017-08-03 10:45:00
2017-08-04 10:00:00 ~ 2017-08-04 10:45:00
2017-08-05 10:00:00 ~ 2017-08-05 10:45:00
2017-08-06 10:00:00 ~ 2017-08-06 10:45:00
2017-08-07 10:00:00 ~ 2017-08-07 10:45:00
2017-08-08 10:00:00 ~ 2017-08-08 10:45:00
2017-08-09 10:00:00 ~ 2017-08-09 10:45:00
2017-08-10 10:00:00 ~ 2017-08-10 10:45:00
2017-08-11 10:00:00 ~ 2017-08-11 10:45:00
2017-08-12 10:00:00 ~ 2017-08-12 10:45:00
2017-08-13 10:00:00 ~ 2017-08-13 10:45:00
2017-08-14 10:00:00 ~ 2017-08-14 10:45:00
2017-08-15 10:00:00 ~ 2017-08-15 10:45:00
2017-08-16 10:00:00 ~ 2017-08-16 10:45:00
2017-08-17 10:00:00 ~ 2017-08-17 10:45:00
2017-08-18 10:00:00 ~ 2017-08-18 10:45:00
2017-08-19 10:00:00 ~ 2017-08-19 10:45:00
2017-08-20 10:00:00 ~ 2017-08-20 10:45:00
2017-08-21 10:00:00 ~ 2017-08-21 10:45:00
2017-08-22 10:00:00 ~ 2017-08-22 10:45:00
2017-08-23 10:00:00 ~ 2017-08-23 10:45:00
2017-08-24 10:00:00 ~ 2017-08-24 10:45:00
2017-08-25 10:00:00 ~ 2017-08-25 10:45:00
2017-08-26 10:00:00 ~ 2017-08-26 10:45:00
2017-08-27 10:00:00 ~ 2017-08-27 10:45:00
2017-08-28 10:00:00 ~ 2017-08-28 10:45:00
2017-08-29 10:00:00 ~ 2017-08-29 10:45:00
2017-08-30 10:00:00 ~ 2017-08-30 10:45:00
2017-08-31 10:00:00 ~ 2017-08-31 10:45:00
2017-09-01 10:00:00 ~ 2017-09-01 10:45:00
2017-09-02 10:00:00 ~ 2017-09-02 10:45:00
2017-09-03 10:00:00 ~ 2017-09-03 10:45:00
2017-09-04 10:00:00 ~ 2017-09-04 10:45:00
2017-09-05 10:00:00 ~ 2017-09-05 10:45:00
2017-09-06 10:00:00 ~ 2017-09-06 10:45:00
2017-09-07 10:00:00 ~ 2017-09-07 10:45:00
2017-09-08 10:00:00 ~ 2017-09-08 10:45:00
2017-09-09 10:00:00 ~ 2017-09-09 10:45:00
2017-09-10 10:00:00 ~ 2017-09-10 10:45:00
2017-09-11 10:00:00 ~ 2017-09-11 10:45:00
2017-09-12 10:00:00 ~ 2017-09-12 10:45:00
2017-09-13 10:00:00 ~ 2017-09-13 10:45:00
2017-09-14 10:00:00 ~ 2017-09-14 10:45:00
2017-09-15 10:00:00 ~ 2017-09-15 10:45:00
2017-09-16 10:00:00 ~ 2017-09-16 10:45:00
2017-09-17 10:00:00 ~ 2017-09-17 10:45:00
2017-09-18 10:00:00 ~ 2017-09-18 10:45:00
2017-09-19 10:00:00 ~ 2017-09-19 10:45:00
2017-09-20 10:00:00 ~ 2017-09-20 10:45:00
2017-09-21 10:00:00 ~ 2017-09-21 10:45:00
2017-09-22 10:00:00 ~ 2017-09-22 10:45:00
2017-09-23 10:00:00 ~ 2017-09-23 10:45:00
2017-09-24 10:00:00 ~ 2017-09-24 10:45:00
2017-09-25 10:00:00 ~ 2017-09-25 10:45:00
2017-09-26 10:00:00 ~ 2017-09-26 10:45:00
2017-09-27 10:00:00 ~ 2017-09-27 10:45:00
2017-09-28 10:00:00 ~ 2017-09-28 10:45:00
2017-09-29 10:00:00 ~ 2017-09-29 10:45:00
2017-09-30 10:00:00 ~ 2017-09-30 10:45:00
2017-10-01 10:00:00 ~ 2017-10-01 10:45:00
2017-10-02 10:00:00 ~ 2017-10-02 10:45:00
2017-10-03 10:00:00 ~ 2017-10-03 10:45:00
2017-10-04 10:00:00 ~ 2017-10-04 10:45:00
2017-10-05 10:00:00 ~ 2017-10-05 10:45:00
2017-10-06 10:00:00 ~ 2017-10-06 10:45:00
2017-10-07 10:00:00 ~ 2017-10-07 10:45:00
2017-10-08 10:00:00 ~ 2017-10-08 10:45:00
2017-10-09 10:00:00 ~ 2017-10-09 10:45:00
2017-10-10 10:00:00 ~ 2017-10-10 10:45:00
2017-10-11 10:00:00 ~ 2017-10-11 10:45:00
2017-10-12 10:00:00 ~ 2017-10-12 10:45:00
2017-10-13 10:00:00 ~ 2017-10-13 10:45:00
2017-10-14 10:00:00 ~ 2017-10-14 10:45:00
2017-10-15 10:00:00 ~ 2017-10-15 10:45:00
2017-10-16 10:00:00 ~ 2017-10-16 10:45:00
2017-10-17 10:00:00 ~ 2017-10-17 10:45:00
2017-10-18 10:00:00 ~ 2017-10-18 10:45:00
2017-10-19 10:00:00 ~ 2017-10-19 10:45:00
2017-10-20 10:00:00 ~ 2017-10-20 10:45:00
2017-10-21 10:00:00 ~ 2017-10-21 10:45:00
2017-10-22 10:00:00 ~ 2017-10-22 10:45:00
2017-10-23 10:00:00 ~ 2017-10-23 10:45:00
2017-10-24 10:00:00 ~ 2017-10-24 10:45:00
2017-10-25 10:00:00 ~ 2017-10-25 10:45:00
2017-10-26 10:00:00 ~ 2017-10-26 10:45:00
2017-10-27 10:00:00 ~ 2017-10-27 10:45:00
2017-10-28 10:00:00 ~ 2017-10-28 10:45:00
2017-10-29 10:00:00 ~ 2017-10-29 10:45:00
2017-10-30 10:00:00 ~ 2017-10-30 10:45:00
2017-10-31 10:00:00 ~ 2017-10-31 10:45:00
2017-11-01 10:00:00 ~ 2017-11-01 10:45:00
2017-11-02 10:00:00 ~ 2017-11-02 10:45:00
2017-11-03 10:00:00 ~ 2017-11-03 10:45:00
2017-11-04 10:00:00 ~ 2017-11-04 10:45:00
2017-11-05 10:00:00 ~ 2017-11-05 10:45:00
2017-11-06 10:00:00 ~ 2017-11-06 10:45:00
2017-11-07 10:00:00 ~ 2017-11-07 10:45:00
2017-11-08 10:00:00 ~ 2017-11-08 10:45:00
2017-11-09 10:00:00 ~ 2017-11-09 10:45:00
2017-11-10 10:00:00 ~ 2017-11-10 10:45:00
2017-11-11 10:00:00 ~ 2017-11-11 10:45:00
2017-11-12 10:00:00 ~ 2017-11-12 10:45:00
2017-11-13 10:00:00 ~ 2017-11-13 10:45:00
2017-11-14 10:00:00 ~ 2017-11-14 10:45:00
2017-11-15 10:00:00 ~ 2017-11-15 10:45:00
2017-11-16 10:00:00 ~ 2017-11-16 10:45:00
2017-11-17 10:00:00 ~ 2017-11-17 10:45:00
2017-11-18 10:00:00 ~ 2017-11-18 10:45:00
2017-11-19 10:00:00 ~ 2017-11-19 10:45:00
2017-11-20 10:00:00 ~ 2017-11-20 10:45:00
2017-11-21 10:00:00 ~ 2017-11-21 10:45:00
2017-11-22 10:00:00 ~ 2017-11-22 10:45:00
2017-11-23 10:00:00 ~ 2017-11-23 10:45:00
2017-11-24 10:00:00 ~ 2017-11-24 10:45:00
2017-11-25 10:00:00 ~ 2017-11-25 10:45:00
2017-11-26 10:00:00 ~ 2017-11-26 10:45:00
2017-11-27 10:00:00 ~ 2017-11-27 10:45:00
2017-11-28 10:00:00 ~ 2017-11-28 10:45:00
2017-11-29 10:00:00 ~ 2017-11-29 10:45:00
2017-11-30 10:00:00 ~ 2017-11-30 10:45:00
2017-12-01 10:00:00 ~ 2017-12-01 10:45:00
2017-12-02 10:00:00 ~ 2017-12-02 10:45:00
2017-12-03 10:00:00 ~ 2017-12-03 10:45:00
2017-12-04 10:00:00 ~ 2017-12-04 10:45:00
2017-12-05 10:00:00 ~ 2017-12-05 10:45:00
2017-12-06 10:00:00 ~ 2017-12-06 10:45:00
2017-12-07 10:00:00 ~ 2017-12-07 10:45:00
2017-12-08 10:00:00 ~ 2017-12-08 10:45:00
2017-12-09 10:00:00 ~ 2017-12-09 10:45:00
2017-12-10 10:00:00 ~ 2017-12-10 10:45:00
2017-12-11 10:00:00 ~ 2017-12-11 10:45:00
2017-12-12 10:00:00 ~ 2017-12-12 10:45:00
2017-12-13 10:00:00 ~ 2017-12-13 10:45:00
2017-12-14 10:00:00 ~ 2017-12-14 10:45:00
2017-12-15 10:00:00 ~ 2017-12-15 10:45:00
2017-12-16 10:00:00 ~ 2017-12-16 10:45:00
2017-12-17 10:00:00 ~ 2017-12-17 10:45:00
2017-12-18 10:00:00 ~ 2017-12-18 10:45:00
2017-12-19 10:00:00 ~ 2017-12-19 10:45:00
2017-12-20 10:00:00 ~ 2017-12-20 10:45:00
2017-12-21 10:00:00 ~ 2017-12-21 10:45:00
2017-12-22 10:00:00 ~ 2017-12-22 10:45:00
2017-12-23 10:00:00 ~ 2017-12-23 10:45:00
2017-12-24 10:00:00 ~ 2017-12-24 10:45:00
2017-12-25 10:00:00 ~ 2017-12-25 10:45:00
2017-12-26 10:00:00 ~ 2017-12-26 10:45:00
2017-12-27 10:00:00 ~ 2017-12-27 10:45:00
2017-12-28 10:00:00 ~ 2017-12-28 10:45:00
2017-12-29 10:00:00 ~ 2017-12-29 10:45:00
2017-12-30 10:00:00 ~ 2017-12-30 10:45:00
2017-12-31 10:00:00 ~ 2017-12-31 10:45:00
2018-01-01 10:00:00 ~ 2018-01-01 10:45:00
2018-01-02 10:00:00 ~ 2018-01-02 10:45:00
2018-01-03 10:00:00 ~ 2018-01-03 10:45:00
2018-01-04 10:00:00 ~ 2018-01-04 10:45:00
2018-01-05 10:00:00 ~ 2018-01-05 10:45:00
2018-01-06 10:00:00 ~ 2018-01-06 10:45:00
2018-01-07 10:00:00 ~ 2018-01-07 10:45:00
2018-01-08 10:00:00 ~ 2018-01-08 10:45:00
2018-01-09 10:00:00 ~ 2018-01-09 10:45:00
2018-01-10 10:00:00 ~ 2018-01-10 10:45:00
2018-01-11 10:00:00 ~ 2018-01-11 10:45:00
2018-01-12 10:00:00 ~ 2018-01-12 10:45:00
2018-01-13 10:00:00 ~ 2018-01-13 10:45:00
2018-01-14 10:00:00 ~ 2018-01-14 10:45:00
2018-01-15 10:00:00 ~ 2018-01-15 10:45:00
2018-01-16 10:00:00 ~ 2018-01-16 10:45:00
2018-01-17 10:00:00 ~ 2018-01-17 10:45:00
2018-01-18 10:00:00 ~ 2018-01-18 10:45:00
2018-01-19 10:00:00 ~ 2018-01-19 10:45:00
2018-01-20 10:00:00 ~ 2018-01-20 10:45:00
2018-01-21 10:00:00 ~ 2018-01-21 10:45:00
2018-01-22 10:00:00 ~ 2018-01-22 10:45:00
2018-01-23 10:00:00 ~ 2018-01-23 10:45:00
2018-01-24 10:00:00 ~ 2018-01-24 10:45:00
2018-01-25 10:00:00 ~ 2018-01-25 10:45:00
2018-01-26 10:00:00 ~ 2018-01-26 10:45:00
2018-01-27 10:00:00 ~ 2018-01-27 10:45:00
2018-01-28 10:00:00 ~ 2018-01-28 10:45:00
2018-01-29 10:00:00 ~ 2018-01-29 10:45:00
2018-01-30 10:00:00 ~ 2018-01-30 10:45:00
2018-01-31 10:00:00 ~ 2018-01-31 10:45:00
2018-02-01 10:00:00 ~ 2018-02-01 10:45:00
2018-02-02 10:00:00 ~ 2018-02-02 10:45:00
2018-02-03 10:00:00 ~ 2018-02-03 10:45:00
2018-02-04 10:00:00 ~ 2018-02-04 10:45:00
2018-02-05 10:00:00 ~ 2018-02-05 10:45:00
2018-02-06 10:00:00 ~ 2018-02-06 10:45:00
2018-02-07 10:00:00 ~ 2018-02-07 10:45:00
2018-02-08 10:00:00 ~ 2018-02-08 10:45:00
2018-02-09 10:00:00 ~ 2018-02-09 10:45:00
2018-02-10 10:00:00 ~ 2018-02-10 10:45:00
2018-02-11 10:00:00 ~ 2018-02-11 10:45:00
2018-02-12 10:00:00 ~ 2018-02-12 10:45:00
2018-02-13 10:00:00 ~ 2018-02-13 10:45:00
2018-02-14 10:00:00 ~ 2018-02-14 10:45:00
2018-02-15 10:00:00 ~ 2018-02-15 10:45:00
2018-02-16 10:00:00 ~ 2018-02-16 10:45:00
2018-02-17 10:00:00 ~ 2018-02-17 10:45:00
2018-02-18 10:00:00 ~ 2018-02-18 10:45:00
2018-02-19 10:00:00 ~ 2018-02-19 10:45:00
2018-02-20 10:00:00 ~ 2018-02-20 10:45:00
2018-02-21 10:00:00 ~ 2018-02-21 10:45:00
2018-02-22 10:00:00 ~ 2018-02-22 10:45:00
2018-02-23 10:00:00 ~ 2018-02-23 10:45:00
2018-02-24 10:00:00 ~ 2018-02-24 10:45:00
2018-02-25 10:00:00 ~ 2018-02-25 10:45:00
2018-02-26 10:00:00 ~ 2018-02-26 10:45:00
2018-02-27 10:00:00 ~ 2018-02-27 10:45:00
2018-02-28 10:00:00 ~ 2018-02-28 10:45:00
2018-03-01 10:00:00 ~ 2018-03-01 10:45:00
2018-03-02 10:00:00 ~ 2018-03-02 10:45:00
2018-03-03 10:00:00 ~ 2018-03-03 10:45:00
2018-03-04 10:00:00 ~ 2018-03-04 10:45:00
2018-03-05 10:00:00 ~ 2018-03-05 10:45:00
2018-03-06 10:00:00 ~ 2018-03-06 10:45:00
2018-03-07 10:00:00 ~ 2018-03-07 10:45:00
2018-03-08 10:00:00 ~ 2018-03-08 10:45:00
2018-03-09 10:00:00 ~ 2018-03-09 10:45:00
2018-03-10 10:00:00 ~ 2018-03-10 10:45:00
2018-03-11 10:00:00 ~ 2018-03-11 10:45:00
2018-03-12 10:00:00 ~ 2018-03-12 10:45:00
2018-03-13 10:00:00 ~ 2018-03-13 10:45:00
2018-03-14 10:00:00 ~ 2018-03-14 10:45:00
2018-03-15 10:00:00 ~ 2018-03-15 10:45:00
2018-03-16 10:00:00 ~ 2018-03-16 10:45:00
2018-03-17 10:00:00 ~ 2018-03-17 10:45:00
2018-03-18 10:00:00 ~ 2018-03-18 10:45:00
2018-03-19 10:00:00 ~ 2018-03-19 10:45:00
2018-03-20 10:00:00 ~ 2018-03-20 10:45:00
2018-03-21 10:00:00 ~ 2018-03-21 10:45:00
2018-03-22 10:00:00 ~ 2018-03-22 10:45:00
2018-03-23 10:00:00 ~ 2018-03-23 10:45:00
2018-03-24 10:00:00 ~ 2018-03-24 10:45:00
2018-03-25 10:00:00 ~ 2018-03-25 10:45:00
2018-03-26 10:00:00 ~ 2018-03-26 10:45:00
2018-03-27 10:00:00 ~ 2018-03-27 10:45:00
2018-03-28 10:00:00 ~ 2018-03-28 10:45:00
2018-03-29 10:00:00 ~ 2018-03-29 10:45:00
2018-03-30 10:00:00 ~ 2018-03-30 10:45:00
2018-03-31 10:00:00 ~ 2018-03-31 10:45:00
2018-04-01 10:00:00 ~ 2018-04-01 10:45:00
2018-04-02 10:00:00 ~ 2018-04-02 10:45:00
2018-04-03 10:00:00 ~ 2018-04-03 10:45:00
2018-04-04 10:00:00 ~ 2018-04-04 10:45:00
2018-04-05 10:00:00 ~ 2018-04-05 10:45:00
2018-04-06 10:00:00 ~ 2018-04-06 10:45:00
2018-04-07 10:00:00 ~ 2018-04-07 10:45:00
2018-04-08 10:00:00 ~ 2018-04-08 10:45:00
2018-04-09 10:00:00 ~ 2018-04-09 10:45:00
2018-04-10 10:00:00 ~ 2018-04-10 10:45:00
2018-04-11 10:00:00 ~ 2018-04-11 10:45:00
2018-04-12 10:00:00 ~ 2018-04-12 10:45:00
2018-04-13 10:00:00 ~ 2018-04-13 10:45:00
2018-04-14 10:00:00 ~ 2018-04-14 10:45:00
2018-04-15 10:00:00 ~ 2018-04-15 10:45:00
2018-04-16 10:00:00 ~ 2018-04-16 10:45:00
2018-04-17 10:00:00 ~ 2018-04-17 10:45:00
2018-04-18 10:00:00 ~ 2018-04-18 10:45:00
2018-04-19 10:00:00 ~ 2018-04-19 10:45:00
2018-04-20 10:00:00 ~ 2018-04-20 10:45:00
2018-04-21 10:00:00 ~ 2018-04-21 10:45:00
2018-04-22 10:00:00 ~ 2018-04-22 10:45:00
2018-04-23 10:00:00 ~ 2018-04-23 10:45:00
2018-04-24 10:00:00 ~ 2018-04-24 10:45:00
2018-04-25 10:00:00 ~ 2018-04-25 10:45:00
2018-04-26 10:00:00 ~ 2018-04-26 10:45:00
2018-04-27 10:00:00 ~ 2018-04-27 10:45:00
2018-04-28 10:00:00 ~ 2018-04-28 10:45:00
2018-04-29 10:00:00 ~ 2018-04-29 10:45:00
2018-04-30 10:00:00 ~ 2018-04-30 10:45:00
2018-05-01 10:00:00 ~ 2018-05-01 10:45:00
2018-05-02 10:00:00 ~ 2018-05-02 10:45:00
2018-05-03 10:00:00 ~ 2018-05-03 10:45:00
2018-05-04 10:00:00 ~ 2018-05-04 10:45:00
2018-05-05 10:00:00 ~ 2018-05-05 10:45:00
2018-05-06 10:00:00 ~ 2018-05-06 10:45:00
2018-05-07 10:00:00 ~ 2018-05-07 10:45:00
2018-05-08 10:00:00 ~ 2018-05-08 10:45:00
2018-05-09 10:00:00 ~ 2018-05-09 10:45:00
2018-05-10 10:00:00 ~ 2018-05-10 10:45:00
2018-05-11 10:00:00 ~ 2018-05-11 10:45:00
2018-05-12 10:00:00 ~ 2018-05-12 10:45:00
2018-05-13 10:00:00 ~ 2018-05-13 10:45:00
2018-05-14 10:00:00 ~ 2018-05-14 10:45:00
2018-05-15 10:00:00 ~ 2018-05-15 10:45:00
2018-05-16 10:00:00 ~ 2018-05-16 10:45:00
2018-05-17 10:00:00 ~ 2018-05-17 10:45:00
2018-05-18 10:00:00 ~ 2018-05-18 10:45:00
2018-05-19 10:00:00 ~ 2018-05-19 10:45:00
2018-05-20 10:00:00 ~ 2018-05-20 10:45:00
2018-05-21 10:00:00 ~ 2018-05-21 10:45:00
2018-05-22 10:00:00 ~ 2018-05-22 10:45:00
2018-05-23 10:00:00 ~ 2018-05-23 10:45:00
2018-05-24 10:00:00 ~ 2018-05-24 10:45:00
2018-05-25 10:00:00 ~ 2018-05-25 10:45:00
2018-05-26 10:00:00 ~ 2018-05-26 10:45:00
2018-05-27 10:00:00 ~ 2018-05-27 10:45:00
2018-05-28 10:00:00 ~ 2018-05-28 10:45:00
2018-05-29 10:00:00 ~ 2018-05-29 10:45:00
2018-05-30 10:00:00 ~ 2018-05-30 10:45:00
2018-05-31 10:00:00 ~ 2018-05-31 10:45:00
2018-06-01 10:00:00 ~ 2018-06-01 10:45:00
2018-06-02 10:00:00 ~ 2018-06-02 10:45:00
2018-06-03 10:00:00 ~ 2018-06-03 10:45:00
2018-06-04 10:00:00 ~ 2018-06-04 10:45:00
2018-06-05 10:00:00 ~ 2018-06-05 10:45:00
2018-06-06 10:00:00 ~ 2018-06-06 10:45:00
2018-06-07 10:00:00 ~ 2018-06-07 10:45:00
2018-06-08 10:00:00 ~ 2018-06-08 10:45:00
2018-06-09 10:00:00 ~ 2018-06-09 10:45:00
2018-06-10 10:00:00 ~ 2018-06-10 10:45:00
2018-06-11 10:00:00 ~ 2018-06-11 10:45:00
2018-06-12 10:00:00 ~ 2018-06-12 10:45:00
2018-06-13 10:00:00 ~ 2018-06-13 10:45:00
2018-06-14 10:00:00 ~ 2018-06-14 10:45:00
2018-06-15 10:00:00 ~ 2018-06-15 10:45:00
2018-06-16 10:00:00 ~ 2018-06-16 10:45:00
2018-06-17 10:00:00 ~ 2018-06-17 10:45:00
2018-06-18 10:00:00 ~ 2018-06-18 10:45:00
2018-06-19 10:00:00 ~ 2018-06-19 10:45:00
2018-06-20 10:00:00 ~ 2018-06-20 10:45:00
2018-06-21 10:00:00 ~ 2018-06-21 10:45:00
2018-06-22 10:00:00 ~ 2018-06-22 10:45:00
2018-06-23 10:00:00 ~ 2018-06-23 10:45:00
2018-06-24 10:00:00 ~ 2018-06-24 10:45:00
2018-06-25 10:00:00 ~ 2018-06-25 10:45:00
2018-06-26 10:00:00 ~ 2018-06-26 10:45:00
2018-06-27 10:00:00 ~ 2018-06-27 10:45:00
2018-06-28 10:00:00 ~ 2018-06-28 10:45:00
2018-06-29 10:00:00 ~ 2018-06-29 10:45:00
2018-06-30 10:00:00 ~ 2018-06-30 10:45:00
2018-07-01 10:00:00 ~ 2018-07-01 10:45:00
2018-07-02 10:00:00 ~ 2018-07-02 10:45:00
2018-07-03 10:00:00 ~ 2018-07-03 10:45:00
2018-07-04 10:00:00 ~ 2018-07-04 10:45:00
2018-07-05 10:00:00 ~ 2018-07-05 10:45:00
2018-07-06 10:00:00 ~ 2018-07-06 10:45:00
2018-07-07 10:00:00 ~ 2018-07-07 10:45:00
2018-07-08 10:00:00 ~ 2018-07-08 10:45:00
2018-07-09 10:00:00 ~ 2018-07-09 10:45:00
2018-07-10 10:00:00 ~ 2018-07-10 10:45:00
2018-07-11 10:00:00 ~ 2018-07-11 10:45:00
2018-07-12 10:00:00 ~ 2018-07-12 10:45:00
2018-07-13 10:00:00 ~ 2018-07-13 10:45:00
2018-07-14 10:00:00 ~ 2018-07-14 10:45:00
2018-07-15 10:00:00 ~ 2018-07-15 10:45:00
2018-07-16 10:00:00 ~ 2018-07-16 10:45:00
2018-07-17 10:00:00 ~ 2018-07-17 10:45:00
2018-07-18 10:00:00 ~ 2018-07-18 10:45:00
2018-07-19 10:00:00 ~ 2018-07-19 10:45:00
2018-07-20 10:00:00 ~ 2018-07-20 10:45:00
2018-07-21 10:00:00 ~ 2018-07-21 10:45:00
2018-07-22 10:00:00 ~ 2018-07-22 10:45:00
2018-07-23 10:00:00 ~ 2018-07-23 10:45:00
2018-07-24 10:00:00 ~ 2018-07-24 10:45:00
2018-07-25 10:00:00 ~ 2018-07-25 10:45:00
2018-07-26 10:00:00 ~ 2018-07-26 10:45:00
2018-07-27 10:00:00 ~ 2018-07-27 10:45:00
2018-07-28 10:00:00 ~ 2018-07-28 10:45:00
2018-07-29 10:00:00 ~ 2018-07-29 10:45:00
2018-07-30 10:00:00 ~ 2018-07-30 10:45:00
2018-07-31 10:00:00 ~ 2018-07-31 10:45:00
2018-08-01 10:00:00 ~ 2018-08-01 10:45:00
2018-08-02 10:00:00 ~ 2018-08-02 10:45:00
2018-08-03 10:00:00 ~ 2018-08-03 10:45:00
2018-08-04 10:00:00 ~ 2018-08-04 10:45:00
2018-08-05 10:00:00 ~ 2018-08-05 10:45:00
2018-08-06 10:00:00 ~ 2018-08-06 10:45:00
2018-08-07 10:00:00 ~ 2018-08-07 10:45:00
2018-08-08 10:00:00 ~ 2018-08-08 10:45:00
2018-08-09 10:00:00 ~ 2018-08-09 10:45:00
2018-08-10 10:00:00 ~ 2018-08-10 10:45:00
2018-08-11 10:00:00 ~ 2018-08-11 10:45:00
2018-08-12 10:00:00 ~ 2018-08-12 10:45:00
2018-08-13 10:00:00 ~ 2018-08-13 10:45:00
2018-08-14 10:00:00 ~ 2018-08-14 10:45:00
2018-08-15 10:00:00 ~ 2018-08-15 10:45:00
2018-08-16 10:00:00 ~ 2018-08-16 10:45:00
2018-08-17 10:00:00 ~ 2018-08-17 10:45:00
2018-08-18 10:00:00 ~ 2018-08-18 10:45:00
2018-08-19 10:00:00 ~ 2018-08-19 10:45:00
2018-08-20 10:00:00 ~ 2018-08-20 10:45:00
2018-08-21 10:00:00 ~ 2018-08-21 10:45:00
2018-08-22 10:00:00 ~ 2018-08-22 10:45:00
2018-08-23 10:00:00 ~ 2018-08-23 10:45:00
2018-08-24 10:00:00 ~ 2018-08-24 10:45:00
2018-08-25 10:00:00 ~ 2018-08-25 10:45:00
2018-08-26 10:00:00 ~ 2018-08-26 10:45:00
2018-08-27 10:00:00 ~ 2018-08-27 10:45:00
2018-08-28 10:00:00 ~ 2018-08-28 10:45:00
2018-08-29 10:00:00 ~ 2018-08-29 10:45:00
2018-08-30 10:00:00 ~ 2018-08-30 10:45:00
2018-08-31 10:00:00 ~ 2018-08-31 10:45:00
2018-09-01 10:00:00 ~ 2018-09-01 10:45:00
2018-09-02 10:00:00 ~ 2018-09-02 10:45:00
2018-09-03 10:00:00 ~ 2018-09-03 10:45:00
2018-09-04 10:00:00 ~ 2018-09-04 10:45:00
2018-09-05 10:00:00 ~ 2018-09-05 10:45:00
2018-09-06 10:00:00 ~ 2018-09-06 10:45:00
2018-09-07 10:00:00 ~ 2018-09-07 10:45:00
2018-09-08 10:00:00 ~ 2018-09-08 10:45:00
2018-09-09 10:00:00 ~ 2018-09-09 10:45:00
2018-09-10 10:00:00 ~ 2018-09-10 10:45:00
2018-09-11 10:00:00 ~ 2018-09-11 10:45:00
2018-09-12 10:00:00 ~ 2018-09-12 10:45:00
2018-09-13 10:00:00 ~ 2018-09-13 10:45:00
2018-09-14 10:00:00 ~ 2018-09-14 10:45:00
2018-09-15 10:00:00 ~ 2018-09-15 10:45:00
2018-09-16 10:00:00 ~ 2018-09-16 10:45:00
2018-09-17 10:00:00 ~ 2018-09-17 10:45:00
2018-09-18 10:00:00 ~ 2018-09-18 10:45:00
2018-09-19 10:00:00 ~ 2018-09-19 10:45:00
2018-09-20 10:00:00 ~ 2018-09-20 10:45:00
2018-09-21 10:00:00 ~ 2018-09-21 10:45:00
2018-09-22 10:00:00 ~ 2018-09-22 10:45:00
2018-09-23 10:00:00 ~ 2018-09-23 10:45:00
2018-09-24 10:00:00 ~ 2018-09-24 10:45:00
2018-09-25 10:00:00 ~ 2018-09-25 10:45:00
2018-09-26 10:00:00 ~ 2018-09-26 10:45:00
2018-09-27 10:00:00 ~ 2018-09-27 10:45:00
2018-09-28 10:00:00 ~ 2018-09-28 10:45:00
<think>好的,我现在要解决用户的问题:在Java中如何拼接两个List。首先,我需要回想一下Java中List的基本操作相关类库。用户之前问过关于slice的问题,现在转向拼接,可能是在处理集合操作时遇到了问题。 首先,用户可能想知道如何将两个List合并成一个新的List。需要考虑到不同的场景,比如是否允许修改原List,是否需要线程安全,或者如何处理不同类型的List(如ArrayListLinkedList)。常见的拼接方法可能有几种,我需要逐一分析。 第一种方法是使用addAll()方法。这是最直接的方式,通过将第二个List的所有元素添加到第一个List中。但要注意,这会修改第一个List,如果用户不希望原List被改变,可能需要先创建一个新的List,再将两个List的元素都添加进去。例如,new ArrayList<>(list1)然后addAll(list2)。 第二种方法是使用Stream API,特别是Java 8及以上版本。通过Stream的concat()方法将两个List的流合并,然后收集成新的List。这种方法的好处是不会修改原始List,并且代码简洁,适合函数式编程风格。 第三种方法是使用第三方库,比如Apache Commons Collections或Guava。这些库提供了ListUtils.union()或Iterables.concat()方法,可能更高效或更方便,但需要引入外部依赖,用户可能不希望这样。 接下来,需要考虑各种方法的注意事项。例如,addAll()方法对于ArrayListLinkedList的性能差异。ArrayList的addAll()在尾部添加时较快,而LinkedList可能每次都需要遍历。另外,如果List中包含null元素,这些方法是否支持,应该都是允许的,因为List允许null。 还要提到不可变List的情况,比如使用List.of()创建的不可变集合,无法直接修改,所以必须用Stream或新建List的方式拼接。此外,Java 10的List.copyOf()也可能涉及不可变集合的处理。 然后需要对比不同方法的优缺点。比如,addAll()方法简单但会修改原List;Stream API不修改原数据但可能性能稍差;第三方库方法方便但需要依赖。 可能还需要提到线程安全的问题,比如如果两个List可能被多个线程修改,拼接时需要注意同步,或者使用并发集合类。 用户可能的深层需求是希望高效且安全地合并两个List,可能用于数据处理或集合操作。需要确保回答覆盖了常见的使用场景,并提供代码示例,让用户能够直接应用。 最后,总结各个方法,并给出建议,根据不同的情况选择合适的方法。比如,如果允许修改原List,使用addAll();如果需要保持原List不变,使用Stream或新建List;如果项目中有Guava库,可以使用其提供的方法。</think>在 Java 中,拼接两个 `List` 的常见方法有以下几种,具体选择取决于是否需要保留原列表、性能需求及代码简洁性: --- ### **1. 使用 `addAll()` 方法(修改原列表)** **特点**: - 将第二个列表的元素**追加到第一个列表末尾**,**直接修改第一个列表**。 - 适用于允许修改原列表的场景。 **代码示例**: ```java List<String> list1 = new ArrayList<>(Arrays.asList("A", "B")); List<String> list2 = new ArrayList<>(Arrays.asList("C", "D")); list1.addAll(list2); // 直接修改 list1 System.out.println(list1); // 输出 [A, B, C, D] ``` --- ### **2. 创建新列表(不修改原列表)** **特点**: - **生成新列表**,原列表保持不变。 - 适用于需要保留原列表独立性的场景。 **代码示例**: ```java List<String> list1 = Arrays.asList("A", "B"); List<String> list2 = Arrays.asList("C", "D"); // 创建新列表并合并 List<String> mergedList = new ArrayList<>(); mergedList.addAll(list1); mergedList.addAll(list2); System.out.println(mergedList); // 输出 [A, B, C, D] ``` --- ### **3. 使用 Java 8+ Stream API(推荐)** **特点**: - 通过 `Stream.concat()` 合并两个流,再转换为列表。 - 代码简洁,且**不修改原列表**。 **代码示例**: ```java List<String> list1 = Arrays.asList("A", "B"); List<String> list2 = Arrays.asList("C", "D"); List<String> mergedList = Stream.concat(list1.stream(), list2.stream()) .collect(Collectors.toList()); System.out.println(mergedList); // 输出 [A, B, C, D] ``` --- ### **4. 使用第三方库(如 Apache Commons 或 Guava)** #### **Apache Commons Collections** ```java List<String> mergedList = ListUtils.union(list1, list2); ``` #### **Guava** ```java List<String> mergedList = ImmutableList.<String>builder() .addAll(list1) .addAll(list2) .build(); ``` --- ### **5. 处理不可变列表(Java 9+)** 如果原列表是 `List.of()` 创建的不可变列表,需通过新建列表合并: ```java List<String> list1 = List.of("A", "B"); List<String> list2 = List.of("C", "D"); List<String> mergedList = new ArrayList<>(list1); mergedList.addAll(list2); ``` --- ### **注意事项** 1. **浅拷贝问题** 如果列表元素是对象,合并后的列表与原列表共享对象引用,修改元素会影响所有引用它的列表。 2. **性能差异** - `ArrayList.addAll()` 时间复杂度为 $O(n)$,适合频繁操作。 - `LinkedList.addAll()` 在尾部追加时为 $O(1)$(需直接操作节点)。 3. **空值与类型安全** 确保两个列表的泛型类型一致,且允许 `null` 值(除非明确限制)。 --- ### **方法对比** | 方法 | 是否修改原列表 | 性能 | 适用场景 | |--------------------|----------------|-------|----------------------------| | `addAll()` | 是 | 高 | 允许修改原列表 | | 创建新列表 | 否 | 中 | 需保留原列表 | | Stream API | 否 | 中 | Java 8+,代码简洁 | | 第三方库 | 否 | 高 | 已引入依赖的工程 | --- **建议**: - 若需保留原列表,优先选择 **Stream API** 或 **新建列表**。 - 若对性能要求高且允许修改原列表,使用 `addAll()`。 - 避免在循环中频繁合并列表,可能导致内存碎片化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值