431,649 Members | 696 Online
Need help? Post your question and get tips & solutions from a community of 431,649 IT Pros & Developers. It's quick & easy.

# Date/Time part 2

 P: n/a Why is this not returning the right day or month? It gives me 5 when the rightnow variable gives a 18. import java.util.Calendar; public class Test { public static void main(String[] args) { Calendar rightnow; int t; rightnow = Calendar.getInstance(); t = Calendar.DAY_OF_MONTH; System.out.println(rightnow); System.out.println(t); } } Output = java.util.GregorianCalendar[time=1074470212464,areFieldsSet=true,areAllField sSet=true,lenient=true,zone=java.util.SimpleTimeZo ne[id=America/Denver,offse t=-25200000,dstSavings=3600000,useDaylight=true,start Year=0,startMode=3,star tMonth=3,startDay=1,startDayOfWeek=1,startTime=720 0000,startTimeMode=0,endMo de=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0],firs tDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2 004,MONTH=0,WEEK_OF_YEAR=4 ,WEEK_OF_MONTH=4,DAY_OF_MONTH=18,DAY_OF_YEAR=18,DA Y_OF_WEEK=1,DAY_OF_WEEK_IN _MONTH=3,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=56,S ECOND=52,MILLISECOND=464,Z ONE_OFFSET=-25200000,DST_OFFSET=0] 5 Jul 17 '05 #1
2 Replies

 P: n/a "Rob" wrote in message news:... Why is this not returning the right day or month? It gives me 5 when the rightnow variable gives a 18. import java.util.Calendar; public class Test { public static void main(String[] args) { Calendar rightnow; int t; rightnow = Calendar.getInstance(); t = Calendar.DAY_OF_MONTH; System.out.println(rightnow); System.out.println(t); } } Output = java.util.GregorianCalendar[time=1074470212464,areFieldsSet=true,areAllField sSet=true,lenient=true,zone=java.util.SimpleTimeZo ne[id=America/Denver,offse t=-25200000,dstSavings=3600000,useDaylight=true,start Year=0,startMode=3,star tMonth=3,startDay=1,startDayOfWeek=1,startTime=720 0000,startTimeMode=0,endMo de=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0],firs tDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2 004,MONTH=0,WEEK_OF_YEAR=4 ,WEEK_OF_MONTH=4,DAY_OF_MONTH=18,DAY_OF_YEAR=18,DA Y_OF_WEEK=1,DAY_OF_WEEK_IN _MONTH=3,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=56,S ECOND=52,MILLISECOND=464,Z ONE_OFFSET=-25200000,DST_OFFSET=0] 5 Calendar.DAY_OF_MONTH is a constant with a value of 5. Greg Jul 17 '05 #2

 P: n/a "Gregory A. Swarthout" wrote in message news:5c**************************@posting.google.c om... "Rob" wrote in message news:... Why is this not returning the right day or month? It gives me 5 when the rightnow variable gives a 18. import java.util.Calendar; public class Test { public static void main(String[] args) { Calendar rightnow; int t; rightnow = Calendar.getInstance(); t = Calendar.DAY_OF_MONTH; System.out.println(rightnow); System.out.println(t); } } Output = java.util.GregorianCalendar[time=1074470212464,areFieldsSet=true,areAllField sSet=true,lenient=true,zone=java.util.SimpleTimeZo ne[id=America/Denver,offse t=-25200000,dstSavings=3600000,useDaylight=true,start Year=0,startMode=3,star tMonth=3,startDay=1,startDayOfWeek=1,startTime=720 0000,startTimeMode=0,endMo de=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime=7200000,endTimeMode=0],firs tDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2 004,MONTH=0,WEEK_OF_YEAR=4 ,WEEK_OF_MONTH=4,DAY_OF_MONTH=18,DAY_OF_YEAR=18,DA Y_OF_WEEK=1,DAY_OF_WEEK_IN _MONTH=3,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=56,S ECOND=52,MILLISECOND=464,Z ONE_OFFSET=-25200000,DST_OFFSET=0] 5 Calendar.DAY_OF_MONTH is a constant with a value of 5. Greg In other words, you didn't ask it to return the right day or month or anything. You asked it to show the value of Calendar.DAY_OF_MONTH, which Greg pointed out has a value of 5. What you're looking for is something like: System.out.println(t.get(Calendar.DAY_OF_MONTH)); I didn't look it up, but I think that's it. Your Calendar object is t. The get(int something) method of Calendar returns the specified part of the calendar. You specify which part by using the constants defined in Calendar. Jul 17 '05 #3

### This discussion thread is closed

Replies have been disabled for this discussion.