工作日的Java日历无法正常工作

时间:2010-05-26 23:36:50

标签: java calendar

我想在2010年5月16日(星期日)和5月25日(星期二)之间输出所有工作日(MON-FRI)。正确的输出应为17,18,19,20,21,24,25。但是,得到的结果是17,18,19,20,21,17,18,19。其他方法只是将日期所在的字符串拆分为

import java.util.*;
public class test
{
    public static void main(String[] args) {

        String startTime = "5/16/2010 11:44 AM";
        String endTime = "5/25/2010 12:00 PM";
        GregorianCalendar startCal = new GregorianCalendar();
        startCal.setLenient(true);
        String[] start = splitString(startTime);   
        //this sets year, month day
        startCal.set(Integer.parseInt(start[2]),Integer.parseInt(start[0])-1,Integer.parseInt(start[1]));
        startCal.set(GregorianCalendar.HOUR, Integer.parseInt(start[3]));
        startCal.set(GregorianCalendar.MINUTE, Integer.parseInt(start[4]));
        if (start[5].equalsIgnoreCase("AM")) { startCal.set(GregorianCalendar.AM_PM, 0); }
        else { startCal.set(GregorianCalendar.AM_PM, 1); }

        GregorianCalendar endCal = new GregorianCalendar();
        endCal.setLenient(true);
        String[] end = splitString(endTime);
        endCal.set(Integer.parseInt(end[2]),Integer.parseInt(end[0])-1,Integer.parseInt(end[1]));
        endCal.set(GregorianCalendar.HOUR, Integer.parseInt(end[3]));
        endCal.set(GregorianCalendar.MINUTE, Integer.parseInt(end[4]));
        if (end[5].equalsIgnoreCase("AM")) { endCal.set(GregorianCalendar.AM_PM, 0); }
        else { endCal.set(GregorianCalendar.AM_PM, 1); }


        for (int i = startCal.get(Calendar.DATE); i < endCal.get(Calendar.DATE); i++)
        {
            startCal.set(Calendar.DATE, i);
            startCal.set(Calendar.DAY_OF_WEEK, i);
            if (startCal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.TUESDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.WEDNESDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.THURSDAY || startCal.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)
            {
                System.out.println("\t" + startCal.get(Calendar.DATE));
            }
        }
    }

    private static String[] splitDate(String date)
    {
        String[] temp1 = date.split(" "); // split by space
        String[] temp2 = temp1[0].split("/"); // split by /
        //5/21/2010 10:00 AM
        return temp2; // return 5 21 2010 in one array
    }

    private static String[] splitTime(String date)
    {
        String[] temp1 = date.split(" "); // split by space
        String[] temp2 = temp1[1].split(":"); // split by :
        //5/21/2010 10:00 AM
        String[] temp3 = {temp2[0], temp2[1], temp1[2]};
        return temp3; // return 10 00 AM in one array
    }

    private static String[] splitString(String date)
    {
        String[] temp1 = splitDate(date);
        String[] temp2 = splitTime(date);
        String[] temp3 = new String[6];
        return dateFill(temp3, temp2[0], temp2[1], temp2[2], temp1[0], temp1[1], temp1[2]);
    }

    private static String[] dateFill(String[] date, String hours, String minutes, String ampm, String month, String day, String year) {
        date[0] = month;
        date[1] = day;
        date[2] = year;
        date[3] = hours;
        date[4] = minutes;
        date[5] = ampm;
        return date;
    }

    private String dateString(String[] date) {
        //return month+" "+day+", "+year+" "+hours+":"+minutes+" "+ampm
        //5/21/2010 10:00 AM
        return date[3]+"/"+date[4]+"/ "+date[5]+" "+date[0]+":"+date[1]+" "+date[2];
    }
}

4 个答案:

答案 0 :(得分:1)

这段代码不好。

我不明白你为什么要对字符串进行所有这些解析来获取Date,反之亦然,当你有java.text.DateFormat和java.text.SimpleDateFormat为你轻松完成时。

我认为这更好。看看你是否同意:

package com.contacts.util;

import java.text.DateFormat;
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 DateUtils
{
    private static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat("dd-MMM-yyyy");

    public static void main(String[] args)
    {
        try
        {
            Date startDate = ((args.length > 0) ? DEFAULT_FORMAT.parse(args[0]) : new Date());
            Date endDate   = ((args.length > 1) ? DEFAULT_FORMAT.parse(args[1]) : new Date());

            List<Date> weekdays = DateUtils.getWeekdays(startDate, endDate);
            Calendar calendar = Calendar.getInstance();
            for (Date d : weekdays)
            {
                calendar.setTime(d);
                int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
                int month = calendar.get(Calendar.MONTH);
                int year = calendar.get(Calendar.YEAR);
    //          System.out.println(DEFAULT_FORMAT.format(d));
                System.out.println("day: " + dayOfMonth + " month: " + (month+1) + " year: " + year);
            }
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }
    }

    public static List<Date> getWeekdays(Date startDate, Date endDate)
    {
        List<Date> weekdays = new ArrayList<Date>();

        if ((startDate == null) || (endDate == null))
            return weekdays;

        if (startDate.equals(endDate))
        {
            if (isWeekday(startDate))
            {
                weekdays.add(startDate);
            }
        }
        else if (startDate.after(endDate))
        {
            weekdays = getWeekdays(endDate, startDate);
        }
        else
        {
            Calendar calendar = Calendar.getInstance();

            calendar.setTime(startDate);
            Date d = startDate;
            while (endDate.equals(d) || endDate.after(d))
            {
                if (isWeekday(d))
                {
                    weekdays.add(d);
                }

                calendar.add(Calendar.DATE, 1);
                d = calendar.getTime();

            }
        }

        return weekdays;
    }

    public static boolean isWeekday(Date d)
    {
        if (d == null)
            return false;

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        return ((dayOfWeek >= Calendar.MONDAY) && (dayOfWeek <= Calendar.FRIDAY));
    }
}

答案 1 :(得分:1)

startCal.set(Calendar.DAY_OF_WEEK, i);每7个循环将翻转你的日期。

答案 2 :(得分:0)

我不知道这是否是您的代码的问题,但JDK对Calendar常量使用了一些意外的值。例如,月份为零。换句话说,Calendar.JANUARY为0.另一方面,工作日为1到7,从星期日开始为1.等等。

答案 3 :(得分:0)

我很幸运地对Java中的Date知之甚少,但我知道它基本上是一个困难和糟糕的API。去JodaTime,直到新的JSR-310完成。