java - 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"); (int = 0; < list.size(); i++) { employee e = list.get(i); system.out.println(e.getdoj()); } } }
the above codes produce 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 need print date alone. how should change code?
well i'd use joda time (and localdate
class, if want maintain date) rather java.util.calendar
, if do want use calendar
, need 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)); } }
Comments
Post a Comment