iterator Hour - Java java.util

Java examples for java.util:Hour

Description

iterator Hour

Demo Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

public class Main {
    public static SimpleDateFormat sdf = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    public static Iterator<Date> iteratorHour(String begin, int end) {
        List<Date> ls = new ArrayList<Date>();
        try {/*from ww  w . ja  v  a  2s  .  c  o m*/
            Date beginDate = sdf.parse(begin);
            //         System.out.println(beginDate);
            long beginDateTime = beginDate.getTime();
            ls.add(new Date(beginDateTime));
            for (int i = 0; i < end; i++) {
                beginDateTime = beginDateTime + 60 * 60 * 1000;
                ls.add(new Date(beginDateTime));
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

        return ls.iterator();
    }

    public static Iterator<Date> iteratorHour(String begin, String end) {
        List<Date> ls = new ArrayList<Date>();

        int endtime = hourRange(begin, end);
        try {
            Date beginDate = sdf.parse(begin);
            //         System.out.println(beginDate);
            long beginDateTime = beginDate.getTime();
            ls.add(new Date(beginDateTime));
            for (int i = 0; i < endtime; i++) {
                beginDateTime = beginDateTime + 60 * 60 * 1000;
                ls.add(new Date(beginDateTime));
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

        return ls.iterator();
    }

    public static Integer hourRange(String begin, String end) {
        Date start = null;
        Date stop = null;
        try {
            start = sdf.parse(begin);
            stop = sdf.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        long range = (stop.getTime() - start.getTime()) / 1000 / 60 / 60;
        return Integer.parseInt(String.valueOf(range));
    }
}

Related Tutorials