天道不一定酬所有勤
但是,天道只酬勤
Hollis出品的全套Java面试宝典不来了解一下吗?

时间的遍历

Hollis出品的全套Java面试宝典不来了解一下吗?
public Calendar getDate(String dateString){  
        int year = Integer.parseInt(dateString.substring(0,dateString.indexOf("-")));  
        int mounth =  Integer.parseInt(dateString.substring(dateString.indexOf("-")+1,dateString.lastIndexOf("-")));  
        int day = Integer.parseInt(dateString.substring(dateString.lastIndexOf("-")+1,dateString.lastIndexOf(" ")));  
        int hour  = Integer.parseInt(dateString.substring(dateString.lastIndexOf(" ")+1,dateString.indexOf(":")));  
        int minutes  = Integer.parseInt(dateString.substring(dateString.indexOf(":")+1,dateString.lastIndexOf(":")));  
        int second  = Integer.parseInt(dateString.substring(dateString.lastIndexOf(":")+1));  
        Calendar date = Calendar.getInstance();  
        date.set(year, mounth-1, day ,hour,minutes,second);  
        return date;  
    }  

Calendar beginDate = getDate("2014-12-31 12:00:00");  
Calendar endDate = getDate("2015-1-31 12:00:00");  
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
while (beginDate.compareTo(endDate) <= 0) {  
    String currentString = sdf.format(beginDate.getTime());  
    sysout(currentString);  
    beginDate.set(Calendar.DATE, beginDate.get(Calendar.DATE) + 1);  
}  
赞(0)
如未加特殊说明,此网站文章均为原创,转载必须注明出处。HollisChuang's Blog » 时间的遍历
Hollis出品的全套Java面试宝典不来了解一下吗?

评论 1

  1. #1

    现在java.time包处理这种问题比较方便了

    sunnymoonue8年前 (2016-07-21)回复

HollisChuang's Blog

联系我关于我