日历api设置许多日期

时间:2010-12-02 09:01:22

标签: java

    import java.util.Calendar;
    import java.util.Date;
    public class Employee {
        private Calendar doj; 
        public Employee(Calendar date) {
            // TODO Auto-generated constructor stub
            this.doj=date;
        }
        public Date getDoj() 
        { 
            Date cal=Calendar.getInstance().getTime();
            return cal; 
        } 

    }

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class TestEmployeeSort {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Employee> coll = getEmployees();
        printList(coll);
    }
        public static List<Employee> getEmployees() 
        { 
            List<Employee> col = new ArrayList<Employee>();
            //SimpleDateFormat format=new SimpleDateFormat("DD/MM/YYY");
            col.add(new Employee(null));
            return col;
        } 
        private static void printList(List<Employee> list) { 
            System.out.println("Date Of Joining"); 

            for (int i = 0; i < list.size(); i++) { 
                Employee e = list.get(i); 
                System.out.println(e.getDoj()); 
            } 
        } 
    }

这将打印当前日期。但我想设置日期,如下所示我使用日期。但我想用日历api

做同样的事情
public static List<Employee> getEmployees()
        { 
            List<Employee> col = new ArrayList<Employee>(); 
            col.add(new Employee(5, "xyz","abc", new Date(1986, 6,12), new Date(1986, 6,12)));
                    return col;
        }

3 个答案:

答案 0 :(得分:2)

您可以使用Calendar.set()方法将日历对象设置为特定的年,月,日。

public static List<Employee> getEmployees()
{ 
    List<Employee> col = new ArrayList<Employee>();
    Calenader cal1 = Calendar.getInstance();
    cal1.set(1986, 6, 12);
    Calenader cal2 = Calendar.getInstance();
    cal2.set(1986, 6, 12);
    col.add(new Employee(5, "xyz","abc", cal1, cal2));
    return col;
}

答案 1 :(得分:0)

我不确定我是否正确理解您的问题:您想使用日历,但是您需要将其设置为特定日期? API表示有一套方法。

答案 2 :(得分:0)

尝试类似

的内容
Calendar c = Calendar.getInstance();
c.set(2000, 1, 10);

System.out.println(c.getTime());