2010-12-02 97 views
-1
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日曆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; 
     } 
+1

我真的不明白你的問題。 – 2010-12-02 09:06:37

回答

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; 
} 
+0

如何在printList()中打印我的意思是我應該在員工類和printList()中做什麼? – Sumithra 2010-12-02 09:36:34

0

我不知道如果我理解你的問題正確:要使用日曆,但你需要將其設置爲一個特定的日期? API說有一個這樣的設置方法。

0

嘗試像

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

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