Java String to Time getTimes(String startTime, String endTime)

Here you can find the source of getTimes(String startTime, String endTime)

Description

get Times

License

Apache License

Declaration

@SuppressWarnings("deprecation")
public static List<String> getTimes(String startTime, String endTime) 

Method Source Code

//package com.java2s;
//License from project: Apache 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 {

    @SuppressWarnings("deprecation")
    public static List<String> getTimes(String startTime, String endTime) {
        List<String> dayList = new ArrayList<String>();
        try {/*  w w  w .  j av a2  s  . co m*/
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = df.parse(startTime);
            Date endDate = df.parse(endTime);
            String now = "";
            for (long i = startDate.getTime(); i <= endDate.getTime(); i += (long) 24
                    * (long) 3600 * (long) 1000) {
                Date date = new Date(i);
                now = date.getYear() + 1900 + "-" + (date.getMonth() + 1)
                        + "-" + date.getDate();
                dayList.add(now);
            }
        } catch (Exception e) {
            //System.out.println(e);   
        }
        return dayList;
    }

    public static Date getTime() {
        try {
            return getDate("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getYear() {
        return getDateString("yyyy");
    }

    public static Date getDate() {
        try {
            return getDate("yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate(String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = new Date(System.currentTimeMillis());
        return convertStringToDate(df.format(date), format);
    }

    public static String getDateString() {
        return getDateString("yyyy-MM-dd");
    }

    public static String getDateString(int after) {
        return addDay(getDateString("yyyy-MM-dd"), after);
    }

    public static String getDateString(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = new Date(System.currentTimeMillis());
        String dateStr = df.format(date);
        return dateStr;
    }

    public static Date convertStringToDate(String time, String format)
            throws ParseException {
        if (format == null) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(time);
    }

    public static String addDay(String beginDateStr, int adddaycount) {
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        try {
            Date beginDate = format.parse(beginDateStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(beginDate);
            cal.add(Calendar.DATE, adddaycount);
            Date enddate = cal.getTime();
            return format.format(enddate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date addDay(Date beginDate, int addcount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);
        cal.add(Calendar.DAY_OF_YEAR, addcount);
        Date enddate = cal.getTime();
        return enddate;
    }
}

Related

  1. getTime(String s)
  2. getTime(String sDateTime, String eDateTime)
  3. getTime(String time)
  4. getTime(String time)
  5. getTimeDate(String time)
  6. replaceDateTime(String input)
  7. str2DateTime(String strDate)
  8. string2Date(String time)
  9. string2DateTime(String stringDate)