calendar api to display date alone
    import java.util.Calendar;
    public class Employee {
        private Calendar doj; 
        public Employee(Calendar date) {
            // TODO Auto-generated constructor stub
            this.doj=date;
        }
        public Calendar getDoj() 
        { 
        return doj; 
        } 
    }
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>();
            col.add(new Employee(Calendar.getInstance()));
            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()); 
            } 
        } 
    }
The above codes produce the following output Date_Of_Joining java.util.GregorianCalendar[time=1291275522078,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=11,WEEK_OF_YEAR=49,WEEK_OF_MONTH=1,DAY_OF_MONTH=2,DAY_OF_YEAR=336,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=8,SECOND=42,MILLISECOND=78,ZONE_OFFSET=19800000,DST_OFFSET=0]
I just need to print the date alone. How should i change the code?
 Well personally I'd use Joda Time (and its LocalDate class, if you really only want to maintain the date) rather than java.util.Calendar , but if you do want to use Calendar , you need a SimpleDateFormat .  
java.util.Calendar sample:
import java.util.*;
import java.text.*;
public class Test
{
    public static void main(String[] args)
    {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        System.out.println(format.format(calendar.getTime()));
    }
}
Joda Time sample:
import org.joda.time.*;
import org.joda.time.format.*;
public class Test
{
    public static void main(String[] args)
    {
        LocalDate today = new LocalDate();
        // Alternatively, use DateTimeFormat.mediumDate etc
        DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy");
        System.out.println(formatter.print(today));
    }
}
使用java.text.DateFormat而不是裸露的Calendar 。 
use Caledar.getTime() that returns Date. Then use SimpleDateFormat to format it as you wish.
链接地址: http://www.djcxy.com/p/3090.html下一篇: 日历api单独显示日期
