Here is my code to fetch and display date:
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 code produces 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]
What I want to do is simply to print the date alone. Here, my output contains way too much data that I don’t need. How should I change the code to achieve this?
Well personally I’d use Joda Time (and its
LocalDateclass, if you really only want to maintain the date) rather thanjava.util.Calendar, but if you do want to useCalendar, you need aSimpleDateFormat.java.util.Calendar sample:
Joda Time sample: