日历api单独显示日期

    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()); 
            } 
        } 
    }

以上代码产生以下输出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 =假,过渡= 6,lastRule =空],Firstdayofweek可= 1,minimalDaysInFirstWeek = 1,ERA = 1,YEAR = 2010,月= 11,WEEK_OF_YEAR = 49,WE​​EK_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,微差= 78,ZONE_OFFSET = 19800000,DST_OFFSET = 0]

我只需要单独打印日期。 我应该如何更改代码?


那么我个人会使用乔达时间(和它的LocalDate类,如果你真的只想维护日期)而不是java.util.Calendar ,但如果你想使用Calendar ,你需要一个SimpleDateFormat

java.util.Calendar示例:

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()));
    }
}

乔达时间样本:

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


使用返回Date的Caledar.getTime()。 然后使用SimpleDateFormat根据需要对其进行格式化。

链接地址: http://www.djcxy.com/p/3089.html

上一篇: calendar api to display date alone

下一篇: Is there something wrong with SimpleDateFormat year and timezone formating