Java Time Parse getTimeList(String start, String end, int pitch)

Here you can find the source of getTimeList(String start, String end, int pitch)

Description

get Time List

License

Open Source License

Declaration

public static List<String> getTimeList(String start, String end, int pitch) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;

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

import java.util.List;

public class Main {

    public static List<String> getTimeList(String start, String end, int pitch, String format) {
        List<String> result = new ArrayList<String>();
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        try {//  w w w.  ja v a 2s.co  m
            Date startDate = formatter.parse(start);
            Date endDate = formatter.parse(end);
            Calendar startCalendar = Calendar.getInstance();
            Calendar endCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            endCalendar.setTime(endDate);

            while (startCalendar.before(endCalendar)) {
                result.add(formatter.format(startCalendar.getTime()));
                startCalendar.add(Calendar.MINUTE, pitch);
            }
            if (startCalendar.equals(endCalendar)) {
                result.add(formatter.format(startCalendar.getTime()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static List<String> getTimeList(String start, String end, int pitch) {
        List<String> result = new ArrayList<String>();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        try {
            Date startDate = formatter.parse(start);
            Date endDate = formatter.parse(end);
            Calendar startCalendar = Calendar.getInstance();
            Calendar endCalendar = Calendar.getInstance();
            startCalendar.setTime(startDate);
            endCalendar.setTime(endDate);

            while (startCalendar.before(endCalendar)) {
                result.add(formatter.format(startCalendar.getTime()));
                startCalendar.add(Calendar.MINUTE, pitch);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. getTimeFileName(String suffix)
  2. getTimeFromGermanLocale(String timeString)
  3. getTimeFromJsToJava(String date)
  4. getTimeFromString(String time)
  5. getTimeStr(String date1, String date2)
  6. getTimeWithStr(String source, String pattern)
  7. getTimeX(String dateStr)
  8. isDateTime(String dateTime)