iterator Day - Java java.util

Java examples for java.util:Day

Description

iterator Day

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> iteratorDay(String begin, int end) {
        List<Date> ls = new ArrayList<Date>();
        try {/* w ww.  j a  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 + 24 * 60 * 60 * 1000;
                ls.add(new Date(beginDateTime));
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

        return ls.iterator();
    }

    public static Iterator<Date> iteratorDay(String begin, String end) {
        List<Date> ls = new ArrayList<Date>();
        int endtime = dayRange(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 + 24 * 60 * 60 * 1000;
                ls.add(new Date(beginDateTime));
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

        return ls.iterator();
    }

    public static Integer dayRange(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()) / 24 / 60 / 60
                / 1000;
        return Integer.parseInt(String.valueOf(range));
    }
}

Related Tutorials